struts2 存取cookie

1.首先Action要实现CookiesAware接口,包括:添加类型为Map<String, String>的field(在所示代码中名为cookie),并实现setCookiesMap方法。 

复制代码
  
  
1 public class BasicAction extends ActionSupport implements CookiesAware{ 2 3 ... 4 5   private Map < String, String > cookie; 6 7 @Override public void setCookiesMap(Map < String, String > cookies) { this .cookie = cookies; }
8 9 ... 10 11 }
复制代码

2.在struts.xml中定义cookie拦截器,主要是设置哪些cookie需要被拦截器管理(也就是由拦截器从cookie文件里读出来)。

示例配置:struts会自动读取cityname,username这两个cookie,把它们的值通过setCookiesMap方法装载到相应的cookie map中。

复制代码
1         <interceptors>
2             <interceptor-stack name="myStack">
3                 <interceptor-ref name="cookie">
4                     <param name="cookiesName">xxx,xxx,xxx</param>
5                     <param name="cookiesValue">*</param>
6                 </interceptor-ref>
7                 <interceptor-ref name="defaultStack" />
8             </interceptor-stack>
9         </interceptors>
复制代码

注:

cookiesName是要由拦截器管理的cookie name list,以“,”隔开;同理 cookiesValue是cookie value list;二者都可以设置为*,表示任意值;

defaultStack是struts默认的拦截器栈,需显式添加,否则会报错,有兴趣可以看看它的定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- A complete stack with all the common interceptors in place.
      Generally,  this  stack should be the one you use, though it
      may  do  more than you need. Also, the ordering can be
      switched around (ex:  if  you wish to have your servlet-related
      objects applied before prepare() is called, you'd need to move
      servlet-config interceptor up.
 
      This stack also excludes from the normal validation and workflow
      the method names input, back, and cancel. These typically are
      associated with requests that should not be validated.
      -->
<interceptor-stack name= "defaultStack" >
     <interceptor-ref name= "exception" />
     <interceptor-ref name= "alias" />
     <interceptor-ref name= "servletConfig" />
     <interceptor-ref name= "prepare" />
     <interceptor-ref name= "i18n" />
     <interceptor-ref name= "chain" />
     <interceptor-ref name= "debugging" />
     <interceptor-ref name= "profiling" />
     <interceptor-ref name= "scopedModelDriven" />
     <interceptor-ref name= "modelDriven" />
     <interceptor-ref name= "fileUpload" />
     <interceptor-ref name= "checkbox" />
     <interceptor-ref name= "staticParams" />
     <interceptor-ref name= "params" >
       <param name= "excludeParams" >dojo\..*</param>
     </interceptor-ref>
     <interceptor-ref name= "conversionError" />
     <interceptor-ref name= "validation" >
         <param name= "excludeMethods" >input,back,cancel,browse</param>
     </interceptor-ref>
     <interceptor-ref name= "workflow" >
         <param name= "excludeMethods" >input,back,cancel,browse</param>
     </interceptor-ref>
</interceptor-stack>

另外,由于拦截器是按定义的先后顺序执行,而我在validate()中需要从cookiemap中获得部分用户信息,因此这里 cookie拦截器需在default之前。

3.把上一步定义好的cookie拦截器,指定给要存取cookie的action

两种方式,一是全局配置

1 <default-interceptor-ref name="myStack" />

二是对单个action配置

1 <action...>
2   <interceptor-ref name="myStack">
3   </interceptor-ref>
4  </action>

单独配置会覆盖全局配置,这也是要显式添加defaultStack的原因。

interceptor-ref还可以设置excludeMethods/includeMethods,具体参考http://struts.apache.org/2.0.11/docs/interceptors.html

本以为配置完CookieAware,用cookieMap.add(name,value)就能自动保存cookie了,没想到cookie文件根本没变化,原来 CookieInterceptor只做了读cookie这一步,写就不管了。

CookieInterceptor:The aim of this intercepter is to set values in the stack/action based on cookie name/value of interest.

有兴趣可以看看 CookieInterceptor是怎么实现的。

4.在action中添加cookie的增删方法,通过ServletActionContext.getResponse().addCookie实现

复制代码
 1     public void addCookie(String name,String value){
 2         //创建Cookie
 3          Cookie cookie = new Cookie(name, URLEncoder.encode(value));
 4         //设置Cookie的生命周期
 5          cookie.setMaxAge(60*60*24*365);
 6         ServletActionContext.getResponse().addCookie(cookie);
 7     }
 8     
 9     public void removeCookie(String name){
10         addCookie(name,"TEST");//“TEST”可以是任意非空字段
11     }
12     
13     public String getCookie(String name){
14         String value=cookie.get(name);
15         if(value!=null)
16             return URLDecoder.decode(value);
17         return null;
18     }
复制代码

注:因为在我的应用环境中cookie value是汉字,需要先encode,否则保存时抛异常:

java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value

我没找到删除cookie的方法,所以用addCookie(name,"TEST")间接代替删除方法,这样可以在取出cookie时根据值是否等于TEST来判断cookie是否是删除状态;如果有更好的办法再改进吧

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值