WEBWORK:我写的webwork2中的cookie拦截器
来源:互联网 发布:育知同创吧 编辑:程序博客网 时间:2024/06/06 09:20
我写的webwork2中的cookie拦截器
作者:jscud
转载文章,转载自:
http://www.jscud.com/srun/news/viewhtml/4_2005_1/17.htm
webwork中没有cookie的映射,只有session的映射. 参考了moxie的cookie拦截器,自己写了一个拦截器. :P
作者:jscud
转载文章,转载自:
http://www.jscud.com/srun/news/viewhtml/4_2005_1/17.htm
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;
}
}
- WEBWORK:我写的webwork2中的cookie拦截器
- 我的webwork2的cookie拦截器
- webwork的拦截器- -
- 为struts2中自己实现webwork2中的AroundInterceptor拦截器
- webwork的拦截器的说明
- webWork2.2 拦截器学习笔记!
- WebWork教程- Interceptor(拦截器)
- webwork拦截器使用心得
- WebWork教程- Interceptor(拦截器)
- WebWork教程- Interceptor(拦截器)
- WebWork教程- Interceptor(拦截器)
- webwork拦截器interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation
- WebWork2.2新特性:使用国际化拦截器
- 自定义我的拦截器
- 开始我的WebWork之旅
- JavaScript 学习 - 提高篇
- update
- pdf&mov
- 改善用户体验的alert提示效果
- KMP算法真的搞死人,终于搞懂了
- WEBWORK:我写的webwork2中的cookie拦截器
- C#和.Net的初步研究
- Google Earth地标-美国的18架黑鸟战机
- 美国空军异常壮观的“飞机坟场”
- 今天去笔试,通过了
- LINUX 99问
- 文化与红细胞
- [整理版]ASP常用内置函数
- .net web.config文件详解