我的webwork2的cookie拦截器

类别:Java 点击:0 评论:0 推荐:
webwork中没有cookie的映射,只有session的映射. 参考了moxie的cookie拦截器,自己写了一个拦截器,不知道是不是能用 :P

拦截器代码如下:   package com.cnscud.util.interceptor;   import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.cnscud.util.WebCookies;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import com.opensymphony.xwork.util.OgnlValueStack; /**
 * Cookie拦截器,在Action里增加getWebCookies
 *
 * 参考moxie的cookie测试拦截器.
 *
 * @author scud 2004.10
 *
 */ public class WebCookiesInterceptor extends AroundInterceptor
{     protected void after(ActionInvocation arg0, String arg1) throws Exception
    {     }     protected void before(ActionInvocation arg0) throws Exception
    {
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
       
        Cookie[] cookies = request.getCookies();         if (cookies != null && cookies.length > 0)
        {
            final OgnlValueStack stack = ServletActionContext.getContext().getValueStack();
           
            WebCookies aWCs = new WebCookies(request,response,cookies);                        stack.setValue(WebCookies.KEY_WEBCOOKIES,aWCs);
        }     } }
WebCookies的代码如下:   package com.cnscud.util; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.*; /*
 * WebCookies类
 *
 * 写一个cookie类来代替Cookie就不用依赖javax.servlet.http了 (TODO)
 *
 * @author scud 2004.10
 * 
 */
public class WebCookies
{     /** ValueStack里面的键值 */
    public static final String KEY_WEBCOOKIES = "webCookies";     private HttpServletRequest request;     private HttpServletResponse response;     private Cookie[] cookies;     private Map cookieMap = new HashMap(20);     /**
     * 构造函数
     *
     * @param request
     * @param response
     * @param cookies
     */
    public WebCookies(HttpServletRequest request, HttpServletResponse response,
            Cookie[] cookies)
    {
        this.request = request;
        this.response = response;         setCookies(cookies);
    }     /**
     * 设置Cookie数组
     *
     * @param cookies
     */
    public void setCookies(Cookie[] cookies)
    {
        cookieMap.clear();
        this.cookies = cookies;         for (int i = 0; i < cookies.length; i++)
        {
            Cookie acookie = cookies[i];             cookieMap.put(acookie.getName(), acookie);
        }     }     /**
     * 得到cookie数组
     *
     * @return cookie的数组
     */
    public Cookie[] getCookies()
    {
        return cookies;
    }     /**
     * 得到某个cookie
     *
     * @param sKey 键名
     * @return 对应的cookie
     */
    public Cookie getCookie(String sKey)
    {
        if (null == cookieMap)
        {
            return null;
        }         Object ac = cookieMap.get(sKey);
        if (null != ac)
        {
            return (Cookie) ac;
        }         return null;
    }     public Cookie get(String sKey)
    {
        return getCookie(sKey);
    }
   
    public Map getCookieMap()
    {
        return cookieMap;
    }     /**
     * 添加一个cookie设置到HttpServletResponse中去
     *
     * @param aCookie
     */
    public void addCookie(Cookie aCookie)
    {
        //调用response的addCookie
        if (null != response)
        {
            response.addCookie(aCookie);
        }
    } }   基础Action的代码如下: (其他action继承此Action即可)   package test; import com.cnscud.util.WebCookies;
import com.opensymphony.xwork.ActionSupport;
public class CookieAction extends ActionSupport
{     protected WebCookies webcookies;     public void setWebCookies(WebCookies webcookies)
    {
        this.webcookies = webcookies;
    }
   
   
    public WebCookies getWebCookies()
    {
        return webcookies;
    }
    }
  Xwork的配置如下,可以根据自己的需求改动,我是所有action都截取cookie   <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd"> <xwork>  <include file="webwork-default.xml"/>
 
 <package name="helloWorld" extends="webwork-default">
     <interceptors>
      <interceptor name="webCookiesInterceptor" class="com.cnscud.util.interceptor.WebCookiesInterceptor" />             <interceptor-stack name="cookieStatck">
                <interceptor-ref name="webCookiesInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
     </interceptors>   <default-interceptor-ref name="cookieStatck" />      <action name="startCookie" class="test.StartAction">
      <result name="success" type="dispatcher">
    <param name="location">/start.jsp</param>
   </result>
     </action>
     
     <action name="resultCookie" class="test.ResultAction">
      <result name="success" type="dispatcher">
    <param name="location">/result.jsp</param>
   </result>
     </action>  </package>
 
   
   
</xwork>   result的页面:   <%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="webwork" prefix="ww" %>
<html>
  <head>   
    <title>My JSP 'result.jsp' result page</title>   
  </head>
 
  <body>
  cookie Hello:
  <ww:property   value="webCookies.cookieMap['Hello'].value" />
  <br><br>
   Cookie Hello2:
  <ww:property   value="webCookies.cookieMap.Hello2.value" />
  <br><br>
  testValue:<ww:property   value="testValue" />
  <br><br>
  </body>
</html>

ResultAction代码如下:   package test;
import javax.servlet.http.Cookie;
public class ResultAction extends CookieAction
{
   
    private String testValue;
   
    public String execute() throws Exception
    {
        //for test
        Cookie[] cs = webcookies.getCookies();     
       
       
       
        return SUCCESS;
    }
   
    public void setTestValue(String testValue)
    {
        this.testValue = testValue;
    }
   
    public String getTestValue()
    {
        return testValue;
    }
   
}



StartAction的代码如下


package test; import javax.servlet.http.Cookie;
public class StartAction extends CookieAction
{       public String execute() throws Exception
    {
       
        Cookie ack = new Cookie("Hello","World222");
       
        webcookies.addCookie(ack);
       
        return SUCCESS;
    }
   
   
   
}

本文地址:http://com.8s8s.com/it/it14796.htm