在 Struts 2 和 Struts 中使用 cookie
我有以下(缩短的)struts2 操作:
public class MyAction extends BaseAction implements CookiesAware {
public String execute() {
if (cookiesMap.containsKey("BLAH"))
blah=Integer.parseInt(cookiesMap.get("BLAH"));
return "success";
}
// For handling cookies
Map<String, String> cookiesMap;
@Override
public void setCookiesMap(Map<String, String> cookiesMap) {
this.cookiesMap = cookiesMap;
}
}
当我执行“cookiesMap.containsKey”时,出现空指针异常 - 在我看来,setCookiesMap 没有被调用。我已经实现了 CookiesAware 接口,所以我认为它应该被调用 - 我在这里错过了什么吗?
谢谢
I've got the following (shortened) struts2 action:
public class MyAction extends BaseAction implements CookiesAware {
public String execute() {
if (cookiesMap.containsKey("BLAH"))
blah=Integer.parseInt(cookiesMap.get("BLAH"));
return "success";
}
// For handling cookies
Map<String, String> cookiesMap;
@Override
public void setCookiesMap(Map<String, String> cookiesMap) {
this.cookiesMap = cookiesMap;
}
}
I get a null pointer exception when i do 'cookiesMap.containsKey' - it seems to me that setCookiesMap isn't being called. I've implemented the CookiesAware interface so i would have thought that it should be getting called - have i missed something here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来struts只支持读取cookie,您必须转到servlet响应才能实际设置 cookie。
最后,我选择完全绕过 struts2 cookie 支持,直接进入 servlet 请求/响应对象进行读取和写入:
并且此方法不需要在 struts.xml 或 web.xml 中进行配置,这是一个奖金。所以我对这个解决方案很满意,即使它确实给 struts2 带来了不好的影响。
It appears that struts only supports reading cookies, you have to go to the servlet response to actually set a cookie.
In the end, i've opted to bypass the struts2 cookie support entirely and go directly to the servlet request/response objects for both reading and writing:
And there's no configuration required for this method in either struts.xml or web.xml, which is a bonus. So i'm happy with this solution, even if it does paint struts2 in a poor light.
您还需要为您的操作定义实现 Cookie 拦截器 struts.xml:
You need to also implement the Cookie Interceptor for the action definition in your struts.xml:
虽然我知道这个问题已经存在了 3 年多了,但今天我需要使用 Struts2 设置 cookie,来到这里,并设法以 Struts2-y 方式设置 cookie(使用 2.3.16)。希望这对其他人有帮助。
为了使用 Struts2 设置 cookie,您需要执行以下步骤:
org.apache.struts2.interceptor.CookieProvider
。 (您可能想查看其 javadoc)SetgetCookies();
方法,返回所有你想要设置的cookie。cookieProvider
拦截器与@Pat 在他的回答中提到的方式相同。如果您为 cookie 设置域,则在测试此设置时请确保您请求的是该域下的 URL。就我而言,我没有意识到我是直接访问我的测试机器而不是通过域,并且 cookie 没有被设置。
While I'm aware that the question is now more than 3 years old, today I needed to set a cookie with Struts2, landed here, and managed to set cookies in a Struts2-y way (using 2.3.16). Hope this will help some others.
In order to set cookies with Struts2, you need to follow these steps:
org.apache.struts2.interceptor.CookieProvider
. (You might want to see its javadoc)Set<Cookie> getCookies();
method, returning all the cookies you want to set.cookieProvider
interceptor the same way as @Pat mentioned in his answer.If you set a domain for a cookie, when you test this setup make sure you're requesting a URL under that domain. In my case, I didn't realize I was accessing my test machine directly instead of going through the domain, and the cookie wasn't being set.
以下文章详细介绍了如何使用 cookie 感知接口
http ://www.journaldev.com/2203/how-to-get-servlet-session-request-response-context-attributes-in-struts-2-action
Following article has more details on how to use cookie aware interface
http://www.journaldev.com/2203/how-to-get-servlet-session-request-response-context-attributes-in-struts-2-action