论坛首页 Java企业应用论坛

wekwork2 中对cookie 的操作问题

浏览 7839 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-07-05  
因为Webwork2 中的Action 可以派生于ActionSupport,但是ActionSupport中的ActionContext只能对Paremeter,Session等进行操作,对于cookie和request的attribute 的操作都无能为力!
请教除了在Action中使用ActionServletContext还有其他好的方法吗?
我的问题源于:
用户登录系统后保存的是cookie,而不是seesion,而我又不想每个action中都通过调用ActionServletContext来获取这个cookie的值,因为那样的话,所有的action就等于仍然是绑定在J2EE容器中了,这不是我所希望的,也不是Xwork所希望的!
   发表时间:2004-07-05  
Cookie是保存在Client端的,请问在ActionServletContext中如何获取cookie值?你用了cookie就相当于用了Http Session;用了Webwork2难道想脱离J2EE(准确说是Web)容器去使用?我不太明白。
0 请登录后投票
   发表时间:2004-07-05  
不是想脱离,而是webwork2应该是两步分组成的
webwork2+Xwork

我想你可能不太了解 webwork2
0 请登录后投票
   发表时间:2004-07-05  
Webwork2不是分两分组成,这个说法不准确,应该说Webwork2的Xwork在Web方面的应用,Xwork是核心,WW2只是用ServletDispatcher来调度Http的Request/response,是对Xwork Action处理请求与响应的进一步封装。
0 请登录后投票
   发表时间:2004-07-05  
可以通过interceptor来解耦,比如运行在Web环境下采用WebCookiesInterceptor,运行在Client环境下面采用RemoteFileCookiesInterceptor,Action不用写和cookies相关的代码,定义一个相关的Field,由interceptor负责注入。

这是我的一个做法,不知道有没有其他更好的解决方法。
0 请登录后投票
   发表时间:2004-07-07  
我同意Quake Wang的做法。
我给出示意代码如下,当然实际情况要做相应的修改:
/*
 * Created on 2004-7-7
 * WebCookiesInterceptor.java
 */
package example.util.intercept;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import com.opensymphony.xwork.util.OgnlValueStack;

/**
 * 
 * Web中的Cookie拦截器。
 * 它根据Cookie的名字(name);,将Cookie的值设置到对应的Action的字段中。
 * 
 * @author moxie-qac
 *         achqian@yahoo.com.cn
 */
public class WebCookiesInterceptor extends AroundInterceptor {

	protected void after(ActionInvocation dispatcher, String result); throws Exception {
		
	}

	protected void before(ActionInvocation invocation); throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();;
		Cookie[] cookies = request.getCookies();;
		
		if(cookies != null && cookies.length > 0);{
			final OgnlValueStack stack = ServletActionContext.getContext();.getValueStack();;
			
			for(int i=0 ; i<cookies.length ; i++);{
				stack.setValue(cookies[i].getName();,cookies[i].getValue(););;
			}
		}
		
	}

}

0 请登录后投票
   发表时间:2004-07-07  
moxie 写道
我同意Quake Wang的做法。
我给出示意代码如下,当然实际情况要做相应的修改:
/*
 * Created on 2004-7-7
 * WebCookiesInterceptor.java
 */
package example.util.intercept;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import com.opensymphony.xwork.util.OgnlValueStack;

/**
 * 
 * Web中的Cookie拦截器。
 * 它根据Cookie的名字(name);,将Cookie的值设置到对应的Action的字段中。
 * 
 * @author moxie-qac
 *         achqian@yahoo.com.cn
 */
public class WebCookiesInterceptor extends AroundInterceptor {

	protected void after(ActionInvocation dispatcher, String result); throws Exception {
		
	}

	protected void before(ActionInvocation invocation); throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();;
		Cookie[] cookies = request.getCookies();;
		
		if(cookies != null && cookies.length > 0);{
			final OgnlValueStack stack = ServletActionContext.getContext();.getValueStack();;
			
			for(int i=0 ; i<cookies.length ; i++);{
				stack.setValue(cookies[i].getName();,cookies[i].getValue(););;
			}
		}
		
	}

}



谢谢几位!
却实应该使用拦截器来作,如果在ACTION中使用WEB的类就和STRUTS差不多了。
不知道为什么这里不开一个专门讨论webwork2的论坛呢?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics