struts2获取Session和request方法(转)

在 Struts1.*中,要想访问request、response以及session等Servlet对象是很方便的,因为它们一直是作为形参在各个方法 之间进行传递的,而在Struts2中我们就很难看到它们的芳踪了,因为我们获得表单中的值都是通过预先设置好了的get方法来得到的,那么如果有些参数 我们必须通过request.getParametre或者session.getAttribute来得到,那么应该怎么做呢?按照Max的教程上的说 法,可以分为两种:IoC方式和非IoC方式,如何理解这两种方式的区别呢?IoC是Spring里面的特征之一,字面意思是反转控制,说白了就是依赖注 入,比方说类A依赖类B,那么就主动的给A注入一个类B的对象,下面看一下这两种方法的具体实现。
1.非Ioc方式
这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类,具体的方法如下所示。
获得request对象:

  1. HttpServletRequest   request   =  ServletActionContext . getRequest   () ;
  2. ActionContext   ct =  ActionContext . getContext ()
  3.     HttpServletRequest   request =
  4. ( HttpServletRequest ) ct . get ( ServletActionContext . HTTP_REQUEST ) ;

获得session对象:
在Struts2中底层的session都被封装成了Map类型,我们称之为SessionMap,而平常我们所说的session则是指HttpSession对象,具体的获得方法如下所示。

  1. Map   session = ActionContext . getSession () ;
  2. Map   session = ( Map ) ActionContext . getContext () . getActionContext . SESSION );

得 到这个SessionMap之后我们就可以对session进行读写了,如果我们想得到原始的HttpSession可以首先得到 HttpServletRequest对象,然后通过request.getSession()来取得原始的HttpSession对象。一般情况下 SessionMap已经可以完成所有的工作,我们不必再去碰底层的session了。
2.IoC方式
这种方式相对来说变化就比较少了,具体流程如下所示。
获得request对象:
第一步:让action实现ServletRequestAware接口
第二步:在action中声明一个HttpServletRequest类型的实例变量
第三步:在action中实现ServletRequestAware接口的setServletRequest方法,实现方式很简单,如下所示。
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
获得Session对象(注意,此时的session是SessionMap类型):
第一步:让action实现SessionAware接口
第二步:在action中声明一个HttpServletRequest类型的实例变量
第三步:在action中实现SessionAware接口的setSession方法,实现方式很简单,如下所示。
private Map session;
publicvoid setSession(Map session) {
this. session = session;
}
以下是另一篇关于得到Request和Session的文章:

  1. Struts2 里,如果需要在 Action 中使用 session ,可以通过下面两种方式得到
  2. 1 .通过 ActionContext   class 中的方法 getSession 得到
  3. 2 . Action 实现 org . apache . struts2 . interceptor . SessionAware 接口的方式来对 session 进行操作
  4.  
  5. 下面先看一个采用第一种方式,在 action 中得到 session 的例子
  6. package   s2 . ex . action ;
  7.  
  8.  
  9. import   java . util . Map ;
  10.  
  11.  
  12. import   com . opensymphony . xwork2 . ActionContext ;
  13.  
  14. import   com . opensymphony . xwork2 . ActionSupport ;
  15.  
  16.  
  17. public   class   SessionTestAction   extends   ActionSupport   {  
  18.  
  19.  
  20.      public   String   execute ()   {  
  21.  
  22.       ActionContext   actionContext   =  ActionContext . getContext () ;
  23.  
  24.         Map   session   =  actionContext . getSession () ;
  25.  
  26.         session . put ( " USER_NAME " ,  " Test User " ) ;
  27.  
  28.         return   SUCCESS ;
  29.  
  30.      }  
  31.  
  32. }
  33. 在这个例子中,通过 ActionContext 得到 session ,并往 session 里放置一个 key USER_NAME ,值为 Test   User 的内容。
  34.  
  35. 下面是一个实现 org . apache . struts2 . interceptor . SessionAware 接口来对 session 操作的例子
  36. package   s2 . ex . action ;
  37.  
  38.  
  39. import   java . util . Map ;
  40.  
  41.  
  42. import   org . apache . struts2 . interceptor . SessionAware ;
  43.  
  44.  
  45. import   com . opensymphony . xwork2 . ActionSupport ;
  46.  
  47.  
  48. public   class   SessionTest1Action   extends   ActionSupport   implements SessionAware   {  
  49.  
  50.      private   Map   session ;
  51.  
  52.      public   void   setSession ( Map   session )   {  
  53.  
  54.         this . session   =  session ;
  55.  
  56.  
  57.      }  
  58.  
  59.      public   String   execute ()   {  
  60.  
  61.         this . session . put ( " USER_NAME " ,  " Test User 1 " ) ;
  62.  
  63.         return   SUCCESS ;
  64.  
  65.      }  
  66.  
  67. }
  68.  
  69. 在这个例子中实现了接口 SessionAware 中的 setSession 方法。
  70.  
  71. 上面两种方式都可以得到 session ,能实现的功能都是一样的。
  72. 这里推荐通过第二种方式来使用 session ,原因是便于做单体测试,用第二种方式,只需要构造一个 Map 就可以对 action   class 进行单体测试了。
  73.  
  74.     在一个项目中可能会有很多 action 都需要用到 session ,如果每个 action 都来实现 org . apache . struts2 . interceptor . SessionAware 这个接口,可能会显得比较麻烦,所以建议作一个抽象的 BaseAction 类来实现 org . apache . struts2 . interceptor . SessionAware 接口,以后所有的 action 只要继承这个 BaseAction 就可以了。
  75.  
  76. 下面是一个如何在 JSP 中使用 session 的例子。
  77. <%@  page   contentType = " text/html; charset=UTF-8 "   %>
  78.  
  79. <%@ page   pageEncoding = " utf-8 "   %>
  80.  
  81. <%@ taglib   prefix = " s "   uri = " /struts-tags "   %>
  82.  
  83. < html >
  84.  
  85. < head >
  86.  
  87.     < title > Session   Test </ title >
  88.  
  89. </ head >
  90.  
  91.  
  92. < body >
  93.  
  94. < h1 >< s : property   value = " #session.USER_NAME " /></ h1 >
  95.  
  96. < h1 ></ h1 >
  97.  
  98. </ body >
  99.  
  100. </ html >
  101.  
  102.    一般在项目中往往会往 session 里放置一个 Object ,必如说 user user 里有个 boolean   admin String   userName ,如果 user 里存在 isAdmin 的方法,在 jsp 中可以通过< s : if   test = " #session.user.admin " >来判断用户有没有管理权限,通过< s : property value = " #session.user.userName " >或者来取得用户名。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值