问题描述
我有以下(缩短的)struts2 动作:
I've got the following (shortened) struts2 action:
当我执行cookiesMap.containsKey"时,我得到一个空指针异常——在我看来,setCookiesMap 没有被调用.我已经实现了 CookiesAware 接口,所以我会认为它应该被调用 - 我在这里错过了什么吗?
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?
谢谢
推荐答案
看来struts只支持读取cookies,你得去servlet响应才能真正设置一个cookie.
It appears that struts only supports reading cookies, you have to go to the servlet response to actually set a cookie.
最后,我选择完全绕过 struts2 cookie 支持,直接进入 servlet 请求/响应对象进行读写:
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:
并且在 struts.xml 或 web.xml 中此方法不需要配置,这是一个额外的好处.所以我对这个解决方案很满意,即使它确实在光线不好的情况下绘制 struts2.
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.
这篇关于在 Struts 2 和 Struts 中使用 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!