2012-06-19 8 views
5

Struts2에서 세션에 값을 저장하는 올바른 방법입니까?Session in Struts 2

Map<String, Object> session = ActionContext.getContext().getSession(); 
session.put("user", "USERNAME"); 
+6

이렇게 할 수 있지만 선호하는 방법은 SessionAware 인터페이스를 구현하는 것입니다. [here] (http://struts.apache.org/2.0.6/docs/how-do-weget-access-to-the-session.html) 및 [here] (http : // splinter. com.au/how-to-use-sessions-with-struts-2)를 참조하십시오. – Jasonw

+0

매우 도움이되는 링크, 감사합니다. –

+1

ActionContext에서 세션 객체를 가져 오는 것은 좋은 연습은 아니지만 TheadLocal에서 구현되었으므로 문제가되지 않습니다. –

답변

1

Struts 2.x의 SessionAware 인터페이스 인 Action 클래스는 Action 클래스에 HTTP 세션 동작을 가져 오기 위해 SessionAware 인터페이스를 구현해야합니다.

우리가 SessionAware 인터페이스에서 구현한다면 우리는 액션 클래스의 SessionAware에 의해 setSession() 메소드를 오버라이드해야한다. 우리가 SessionAware 인터페이스로부터 액션 클래스를 구현한다면, 스트럿츠 2 컨트롤러는 정확하게 세션 객체를 주입하지 않지만 비슷한 동작을하는 Map 객체를 주입 할 것입니다.

Map m; 
public void setSession(Map m) 
    { 
     this.m=m; 
    } 

public String execute() 
    { 
     m.put("user", "USERNAME"); 


     return SUCCESS; 
    }