There is no cookie mapping in Webwork, only Session map. Refer to the Moxie's cookie interceptor, write an interceptor, don't know if it can be used: P
The interceptor code is as follows:
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 interceptor, add GetWebCookies in Action
*
* Refer to the Moxie's cookie test interceptor.
*
* @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 && coolies.length> 0)
{
Final OgnvaluestAck Stack = servletActionContext.getContext (). getValUStack ();
Webcookies AWCS = New Webcookies (Request, Response, Cookies);
Stack.SetValue (WebCookies.Key_Webcookies, AWCS);
}
}
}
The code of WebCookies is as follows:
Package com.cnscud.util;
Import java.util.hashmap;
Import java.util.map;
Import javax.servlet.http. *;
/ *
* Webcookies class
*
* Write a cookie class instead of cookies without relying on javax.servlet.http (TODO)
*
* @Author scud 2004.10
*
* /
Public Class Webcookies
{
/ ** VALUESTACK key value * /
Public Static Final String Key_WebCookies = "WebcOokies";
Private httpservletRequest request;
Private httpservletResponse response
PRIVATE COOKIE [] cookies;
Private map cookiemap = new hashmap (20); / **
* Constructor
*
* @Param Request
* @Param Response
* @Param Cookies
* /
Public WebCookies (HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE,
Cookie [] cookies)
{
THIS.REQUEST = REQUEST;
THIS.RESPONSE = Response;
SetCookies (cookies);
}
/ **
* Set the cookie array
*
* @Param Cookies
* /
Public void setCookies (cookie [] cookies)
{
Cookiemap.clear ();
THIS.COOKIES = COOKIES
For (int i = 0; i { Cookie Acookie = Cookies [i]; CookieMap.Put (Acookie.getName (), ACOOKIE); } } / ** * Get a cookie array * * @Return Cookie array * / Public cookie [] getCookies () { Return cookies; } / ** * Get a cookie * * @Param SKEY key name * @Return corresponding 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; } / ** * Add a cookie set to httpservletResponse * * @Param Acookie * / Public Void AddCookie (Cookie ACOOKIE) { // Call Response's AddCookie IF (NULL! = response) { Response.addcookie (ACOOKIE); } } } The code for the base action is as follows: (Other Action inherits this 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; } } If the XWORK configuration is as follows, you can change according to your own needs, I am using all ACTION Cookie interceptor-stack> interceptors> / start.jsp param> result> action> / result.jsp param> result> action> package> xwork> Result's page: <% @ Page ContentType = "Text / HTML; Charset = UTF-8"%> <% @ Taglib Uri = "Webwork" prefix = "ww"%>
hEAD>
Cookie Hello:
Cookie Hello2:
TestValue:
body>
html>
The resultAction code is as follows:
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;
}
}
The code of the StartAction is as follows.
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;
}
}