struts2 获取session的方法,sessionAware 接口的使用

来源:互联网 发布:芒果tv最近新网络剧 编辑:程序博客网 时间:2024/04/19 20:27

  Struts2 的Action中若希望访问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;

    }

 2、实现SessionAware接口:

   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;

    }

    }

使用ActionContext获得session会带来单元测试不好作,所以使用SessionAware可以用通过的Map对象来注入session,

这样在单元测试的时候只需要创建一个Map就可以了。这是一种比较推荐的做法。

在Struts2中,动作类虽然继承 ActionSupport 类,可以直接写我们自己定义的方法,但是却不能像在Struts1中,

对reques / response  / application / HttpServletRequest  等等一些Web元素进行操作,

所以Struts2提供了RequestAware,SessionAware,ApplicationAware/ServletRequestAware....接口.

实现这些接口就可以对其进行想要的操作了.


******************************************************************

Struts2中访问web元素的四种方式及前台jsp页面获取后台值的方式

四种方式:

1. 通过ActionContext来访问request,session,application对象

2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象

3. 通过ServletActionContext来访问request,session,application对象

4. 通过实现ServletRequestAware接口来访问request,session,application对象

 

演示代码:

方式一:

Java代码 
  1. /** 
  2.  * 通过ActionContext来访问request,session,application对象 
  3.  * @author 健 
  4.  */  
  5. public class UserAction1 extends ActionSupport{  
  6.     /** 
  7.      * 序列化 
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     /** 
  11.      * request 
  12.      */  
  13.     private Map <String,Object>request;  
  14.     /** 
  15.      * response 
  16.      */  
  17.     private Map <String,Object>session;  
  18.     /** 
  19.      * application 
  20.      */  
  21.     private Map <String,Object>application;  
  22.     /** 
  23.      * 添加用户  
  24.      * @return 用户是否添加成功 
  25.      */  
  26.     @SuppressWarnings("unchecked")  
  27.     @Override  
  28.     public String execute(){  
  29.         System.out.println("通过ActionContext来访问request,session,application对象");  
  30.         // 初始化  
  31.         request = (Map<String,Object>)ActionContext.getContext().get("request");  
  32.         session = ActionContext.getContext().getSession();  
  33.         application = ActionContext.getContext().getApplication();  
  34.         // 赋值  
  35.         request.put("requestKey""requestValue");  
  36.         session.put("sessionKey""sessionValue");  
  37.         application.put("applicationKey""applicationValue");  
  38.         return "success";  
  39.     }  
  40. }  

方式二:

Java代码 
  1. /** 
  2.  * 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象 
  3.  * @author 健 
  4.  */  
  5. public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{  
  6.     /** 
  7.      * 序列化 
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     /** 
  11.      * request 
  12.      */  
  13.     private Map <String,Object>request;  
  14.     /** 
  15.      * response 
  16.      */  
  17.     private Map <String,Object>session;  
  18.     /** 
  19.      * application 
  20.      */  
  21.     private Map <String,Object>application;  
  22.     /** 
  23.      * 控制器 
  24.      */  
  25.     @SuppressWarnings("unchecked")  
  26.     @Override  
  27.     public String execute(){  
  28.         System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");  
  29.         // 赋值  
  30.         request.put("requestKey""requestValue");  
  31.         session.put("sessionKey""sessionValue");  
  32.         application.put("applicationKey""applicationValue");  
  33.         return "success";  
  34.     }  
  35.     /*  
  36.      * 实现RequestAware中的方法 
  37.      */  
  38.     @Override  
  39.     public void setRequest(Map<String, Object> request) {  
  40.         this.request = request;  
  41.     }  
  42.     /*  
  43.      * 实现ApplicationAware中的方法 
  44.      */  
  45.     @Override  
  46.     public void setApplication(Map<String, Object> application) {  
  47.         this.application = application;  
  48.     }  
  49.     /*  
  50.      * 实现SessionAware中的方法 
  51.      */  
  52.     @Override  
  53.     public void setSession(Map<String, Object> session) {  
  54.         this.session = session;  
  55.     }  
  56. }  

方式三:

Java代码 
  1. +/** 
  2.  * 通过ServletActionContext来访问request,session,application对象 
  3.  * @author 健 
  4.  */  
  5. public class UserAction3 extends ActionSupport{  
  6.     /** 
  7.      * 序列化 
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     /** 
  11.      * request 
  12.      */  
  13.     private HttpServletRequest request;  
  14.     /** 
  15.      * response 
  16.      */  
  17.     private HttpSession session;  
  18.     /** 
  19.      * application 
  20.      */  
  21.     private ServletContext application;  
  22.     /** 
  23.      * 控制器 
  24.      */  
  25.     @SuppressWarnings("unchecked")  
  26.     @Override  
  27.     public String execute(){  
  28.         System.out.println("通过ServletActionContext来访问request,session,application对象");  
  29.         // 初始化  
  30.         request = ServletActionContext.getRequest();  
  31.         session = request.getSession();  
  32.         application = session.getServletContext();  
  33.         // 赋值  
  34.         request.setAttribute("requestKey""requestValue");  
  35.         session.setAttribute("sessionKey""sessionValue");  
  36.         application.setAttribute("applicationKey""applicationValue");  
  37.         return "success";  
  38.     }  
  39. }  

方式四:

Java代码 
  1. /** 
  2.  * 通过实现ServletRequestAware接口来访问request,session,application对象 
  3.  * @author 健 
  4.  */  
  5. public class UserAction4 extends ActionSupport implements ServletRequestAware{  
  6.     /** 
  7.      * 序列化 
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     /** 
  11.      * request 
  12.      */  
  13.     private HttpServletRequest request;  
  14.     /** 
  15.      * response 
  16.      */  
  17.     private HttpSession session;  
  18.     /** 
  19.      * application 
  20.      */  
  21.     private ServletContext application;  
  22.     /** 
  23.      * 控制器 
  24.      */  
  25.     @SuppressWarnings("unchecked")  
  26.     @Override  
  27.     public String execute(){  
  28.         System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");  
  29.         // 赋值  
  30.         request.setAttribute("requestKey""requestValue");  
  31.         session.setAttribute("sessionKey""sessionValue");  
  32.         application.setAttribute("applicationKey""applicationValue");  
  33.         return "success";  
  34.     }  
  35.     /*  
  36.      * 实现ServletRequestAware接口中的方法 
  37.      */  
  38.     @Override  
  39.     public void setServletRequest(HttpServletRequest request) {  
  40.         this.request = request;  
  41.         this.session = request.getSession();  
  42.         this.application = session.getServletContext();  
  43.     }  
  44. }  

上面的action配套的struts.xml及jsp页面

struts.xml

Xhtml代码 
  1. <struts>  
  2.     <!-- 配置开发模式:修改不用重启服务器 -->  
  3.     <constant name="struts.devMode" value="true"/>  
  4.     <package name="" namespace="/login" extends="struts-default">  
  5.         <action name="login*" class="com.wj.struts2.action.UserAction{1}">  
  6.             <result name="success">/success.jsp</result>  
  7.             <result name="failure">/failure.jsp</result>  
  8.         </action>  
  9.     </package>  
  10. </struts>  

index.jsp

Xhtml代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.   
  14.         <title>Struts2_AccessWebElements</title>  
  15.         <meta http-equiv="pragma" content="no-cache">  
  16.         <meta http-equiv="cache-control" content="no-cache">  
  17.         <meta http-equiv="expires" content="0">  
  18.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.         <meta http-equiv="description" content="This is my page">  
  20.         <!-- 
  21.         <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> 
  22.         -->  
  23.         <mce:script language="javascript"><!--  
  24.             function sub(str){  
  25.                 document.form1.action = str;  
  26.                 document.form1.submit();  
  27.             }  
  28.           
  29. // --></mce:script>  
  30.     </head>  
  31.   
  32.     <body>  
  33.         <form name="form1">  
  34.             <div>  
  35.                 Struts2中访问web元素的四种方式<br>  
  36.                 方式一:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>    
  37.                 方式二:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>  
  38.                 方式三:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>  
  39.                 方式四:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>  
  40.             </div>  
  41.         </form>  
  42.     </body>  
  43. </html>  

success.jsp

Xhtml代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.         <title>Struts2_AccessWebElements</title>  
  14.         <meta http-equiv="pragma" content="no-cache">  
  15.         <meta http-equiv="cache-control" content="no-cache">  
  16.         <meta http-equiv="expires" content="0">  
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.         <meta http-equiv="description" content="This is my page">  
  19.         <!-- 
  20.         <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> 
  21.         -->  
  22.     </head>  
  23.   
  24.     <body>  
  25.         requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br>  
  26.         sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br>  
  27.         applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br>  
  28.         --------------------------------------------  
  29.         <s:debug></s:debug>  
  30.     </body>  
  31. </html>  


0 0
原创粉丝点击