struts2访问session的两种方法
??? 1、從ActionContext中獲取;
??? 2、實現SessionAware接口。
??? 1、從ActionContext中獲取:
??? import java.util.Map;
??? import com.opensymphony.xwork2.ActionContext;
??? import com.opensymphony.xwork2.ActionSupport;
??? public class SessionTestAction extends ActionSupport {
??? public String execute() {
??? ActionContext actionContext = ActionContext.getContext();
??? Map session = actionContext.getSession();
??? session.put("USER_NAME", "Test User");
??? return SUCCESS;
??? }
??? }
??? import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class SessionTestAction extends ActionSupport { public String execute() { ActionContext actionContext = ActionContext.getContext(); Map session = actionContext.getSession(); session.put("USER_NAME", "Test User"); return SUCCESS; } }
??? 2、實現SessionAware接口:
??? [java] view plaincopyprint?
??? import java.util.Map;
??? import org.apache.struts2.interceptor.SessionAware;
??? import com.opensymphony.xwork2.ActionSupport;
??? public class SessionTest1Action extends ActionSupport implements SessionAware {
??? private Map session;
??? public void setSession(Map session) {
??? this.session = session;
??? }
??? public String execute() {
??? this.session.put("USER_NAME", "Test User 1");
??? return SUCCESS;
??? }
??? }
??? import java.util.Map; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class SessionTest1Action extends ActionSupport implements SessionAware { private Map session; public void setSession(Map session) { this.session = session; } public String execute() { this.session.put("USER_NAME", "Test User 1"); return SUCCESS; } }
??? 進一步閱讀Struts2.1.8.1源碼,SessionAware接口的實現方式如下:
??? struts-default.xml配置:
??? <interceptors>
??? …
??? <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
??? …
??? </interceptors>
??? <interceptor-stack name="defaultStack">
??? …
??? <interceptor-ref name="servletConfig"/>
??? …
??? </interceptor-stack>
??? <interceptors> … <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/> … </interceptors> <interceptor-stack name="defaultStack"> … <interceptor-ref name="servletConfig"/> … </interceptor-stack>
??? 打開ServletConfigInterceptor.java源碼:
??? public String intercept(ActionInvocation invocation) throws Exception {
??? final Object action = invocation.getAction();
??? final ActionContext context = invocation.getInvocationContext();
??? …
??? if (action instanceof SessionAware) {
??? ((SessionAware) action)。setSession(context.getSession());
??? }
??? …
??? return invocation.invoke();
??? }
??? public String intercept(ActionInvocation invocation) throws Exception { final Object action = invocation.getAction(); final ActionContext context = invocation.getInvocationContext(); … if (action instanceof SessionAware) { ((SessionAware) action)。setSession(context.getSession()); } … return invocation.invoke(); }
??? 即在攔截器處理過程中發現目標Action實現了SessionAware接口,便會調用Action中已經實現的setSession(…) 方法,將ActionContext中包裝的Session注入目標Action中。目標Action也就可以進一步對Session進行操作了。
總結
以上是生活随笔為你收集整理的struts2访问session的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web-yestem(伊思腾)-企业门户
- 下一篇: Struts2中使用Session的两种