실험에 사용 할 도메인은 Employee class


@SessionAttributes(value="employee") 


SessionAttributes alias은 employee 


출력시 어떤 스코프에 있는지 확인을 위해 RequestScope와 SessionScope 둘다 출력 하도록 하였다.


출력페이지 코드


결과 페이지 <br/>

RequestScope : ${requestScope.employee.name } <br/>

SessionScope : ${sessionScope.employee.name }


실험 1.

세션에 아무것도 없을 때


@Controller

@RequestMapping(value="test/*") 

@SessionAttributes(value="employee")

public class TestController {

@RequestMapping(value="test01")

public String test() {

return "forward:resultpage.jsp";

}

}

 

결과





실험 2.

세션에 데이터가 존재 할 때


@Controller

@RequestMapping(value="test/*") 

@SessionAttributes(value="employee")

public class TestController {

@RequestMapping(value="createEmployee")

public String createSession(HttpSession session) {

Employee emp=new Employee();

emp.setName("created Employee");

session.setAttribute("Employee", emp );

return "forward:resultpage.jsp";

}


@RequestMapping(value="test02")

public String test() {

return "forward:resultpage.jsp";

}

}


결과



전통적인 방법으로 세션에 데이터를 입력하였고 결과는 예상대로였다.



세션에 데이터가 있을 경우 실험1과 동일한 요청을 하였을때 

예상과 다르게 requestScope에도 데이터가 있는 것을 확인하였다.


그래서 결과 페이지를 직접 요청해 보았다.

RequestScope에는 존재하지 않는다.


@SessionAttributes를 이용 할 때에 RequestScope에 데이터가 입력된다.




실험3 


실험2와 동일한 조건에서 Model에 같은 이름으로 설정을 해보자.


@Controller

@RequestMapping(value="test/*") 

@SessionAttributes(value="employee")

public class TestController {

@RequestMapping(value="createEmployee")

public String createSession(HttpSession session) {

Employee emp=new Employee();

emp.setName("created Employee");

session.setAttribute("Employee", emp );

return "forward:resultpage.jsp";

}


@RequestMapping(value="test03")

public String test(Model model) {

Employee emp=new Employee();

emp.setName("new Employee");

model.addAttribute("employee",emp);

return "forward:resultpage.jsp";

}

}



결과


세션에 아무것도 없을 경우이다.



세션에 데이터가 입력되는 것을 확인 할 수 있었다.


세션에 데이터가 입력되는 것을 확인 할 수 있었다.



그렇다면 세션에 데이터가 있는 경우는 어떨까

참고로 createEmployee를 먼저 요청 후 test03을 요청하였을때 결과이다.




세션에 데이터가 있던 없던 별 차이가 없는 듯한 결과가 나왔다.



실험4


메소드 인자로 @ModelAttribute를 사용 할 경우.


@Controller

@RequestMapping(value="test/*") 

@SessionAttributes(value="employee")

public class TestController {

@RequestMapping(value="createEmployee")

public String createSession(HttpSession session) {

Employee emp=new Employee();

emp.setName("created Employee");

session.setAttribute("Employee", emp );

return "forward:resultpage.jsp";

}


@RequestMapping(value="test04") 

 public String  test (@ModelAttribute(value="employee")Employee employee) {

     

return "forward:resultpage.jsp";


 }

}


결과

세션에 데이터가 없을 경우

org.springframework.web.HttpSessionRequiredException: Expected session attribute 'employee'


익셉션이 발생한다.


세션에 데이터가 있을 경우





세션에 데이터가 있고 요청에 name을 추가했을 때

?name=test04


세션에 데이터가 없을 경우 익셉션이 발생 하는 것 말고는  

실험3과 같은 것 같다.


실험5


@ModelAtttibute를 메소드 위에 썻을 경우

실험1 부터 같은 조건을 해보았다.


@Controller

@RequestMapping(value="test/*") 

@SessionAttributes(value="employee")

public class TestController {

@ModelAttribute("employee")

public Employee employee() {

Employee emp=new Employee();

emp.setName("default create Employee");

return emp;

}


@RequestMapping(value="createEmployee")

public String createSession(HttpSession session) {

Employee emp=new Employee();

emp.setName("created Employee");

session.setAttribute("Employee", emp );

return "forward:resultpage.jsp";

}


@RequestMapping(value="test01")

public String test() {

return "forward:resultpage.jsp";

}

}


결과

세션에 데이터가 없을 때


@ModelAttribute 가 달린 메소드가 호출 되는 것 같다.


세션에 데이터가 있을 때를 위해 createEmployee를 요청 하였다.



세션을 만드는 메소드가 같은 컨트롤러에 있어서 그런지 위와 같은 결과가 나왔다.

그래서 컨트롤러를 따로 만들어 세션에 데이터 입력 후 다시해 보았다.



 세션에 데이터가 있을 경우는 앞선 실험들과 같은 결과가 나왔다.



실험6

앞선 실험들을 실험5의 @ModelAttribute가 달린 메소드를 추가해서 다시 해보았다.


결과

실험1 부터 3 까지는 같은 결과나왔다. 


※다만 실험4의 세선에 데이터가 없고 메소드인자로 @ModelAttributes가 사용 된 경우 익셉션이 발생하지 않았다. 




총 정리는 이해가 되면 그 때 추가예정.






추가 내용


org.springframework.web.bind.annotation

Annotation Type SessionAttributes



  • @Target(value=TYPE)
     @Retention(value=RUNTIME)
     @Inherited
     @Documented
    public @interface SessionAttributes
    Annotation that indicates the session attributes that a specific handler uses.

    This will typically list the names of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans. Declared at the type level, applying to the model attributes that the annotated handler class operates on.

    NOTE: Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.

    For permanent session attributes, e.g. a user authentication object, use the traditional session.setAttribute method instead. Alternatively, consider using the attribute management capabilities of the generic WebRequest interface.

    NOTE: When using controller interfaces (e.g. for AOP proxying), make sure to consistently put all your mapping annotations — such as @RequestMapping and @SessionAttributes — on the controller interfacerather than on the implementation class.

@SessionAttributes를 사용하였을때 requestescope에도 데이터가 남아있었는데 위에 저 부분이 해당사항인 것으로 추정 되어진다.

그리고 사용자 인증 같은 경우에는 맨날쓰던 session.setAttribute를 쓰라고 권고 하는 듯하다.

총평
애매하다 싶으면 다같이 그냥 쓰던거 쓰자.


잘 못 된 부분이나 더 자세한 정보를 가지고 계신분은 댓글 남겨주시면 감사하겠습니다.


+ Recent posts