Java 在 Struts 2 和 Struts 中使用 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3350554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:30:43  来源:igfitidea点击:

Using cookies with Struts 2 and Struts

javacookiesstruts2

提问by Chris

I've got the following (shortened) struts2 action:

我有以下(缩短的)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;
  }
}

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 shouldbe getting called - have i missed something here?

当我执行 'cookiesMap.containsKey' 时,我得到一个空指针异常 - 在我看来 setCookiesMap 没有被调用。我已经实现了 CookiesAware 接口,所以我认为它应该被调用 - 我在这里错过了什么吗?

Thanks

谢谢

采纳答案by Chris

It appears that struts only supports reading cookies, you have to go to the servlet response to actually seta cookie.

看来struts只支持读取cookies,你必须去servlet响应中实际设置一个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:

最后,我选择完全绕过 struts2 cookie 支持,直接转到 servlet 请求/响应对象进行读取和写入:

public class MyAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

  public int division;

  public String execute() {

    // Load from cookie
    for(Cookie c : servletRequest.getCookies()) {
      if (c.getName().equals("cookieDivision"))
        division=Integer.parseInt(c.getValue());
    }

    // Save to cookie
    Cookie div = new Cookie("cookieDivision", String.format("%d",division));
    div.setMaxAge(60*60*24*365); // Make the cookie last a year
    servletResponse.addCookie(div);

    return "success";
  }

  // For access to the raw servlet request / response, eg for cookies
  protected HttpServletResponse servletResponse;
  @Override
  public void setServletResponse(HttpServletResponse servletResponse) {
    this.servletResponse = servletResponse;
  }

  protected HttpServletRequest servletRequest;
  @Override
  public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;
  }
}

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.xml 或 web.xml 中都不需要配置,这是一个额外的好处。所以我对这个解决方案很满意,即使它确实在光线不足的情况下绘制了 struts2。

回答by Pat

You need to also implement the Cookie Interceptorfor the action definition in your struts.xml:

您还需要为struts.xml 中的操作定义实现Cookie Interceptor

<action name="MyAction" class="your.fancy.app.MyAction">
    <interceptor-ref name="defaultStack"/>       
    <interceptor-ref name="cookie">
        <param name="cookiesName">BLAH</param>
    </interceptor-ref>
    <result>/index.jsp</result>
</action>

回答by orique

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.

虽然我知道这个问题现在已经超过 3 年了,但今天我需要用 Struts2 设置一个 cookie,登陆这里,并设法以 Struts2-y 的方式设置 cookie(使用 2.3.16)。希望这会对其他人有所帮助。

In order to set cookies with Struts2, you need to follow these steps:

为了使用 Struts2 设置 cookie,您需要按照以下步骤操作:

  1. Have your Action implement org.apache.struts2.interceptor.CookieProvider. (You might want to see its javadoc)
  2. Implement the Set<Cookie> getCookies();method, returning all the cookies you want to set.
  3. Make your Action use the cookieProviderinterceptorthe same way as @Pat mentioned in his answer.
  1. 让你的 Action 实现org.apache.struts2.interceptor.CookieProvider。(你可能想看看它的javadoc
  2. 实现该Set<Cookie> getCookies();方法,返回您要设置的所有 cookie。
  3. 让您的 Action以与@Pat 在他的回答中提到的相同方式使用cookieProvider拦截器
<action name="MyAction" class="your.fancy.app.MyAction">
    <interceptor-ref name="defaultStack"/>
    <interceptor-ref name="cookieProvider"/>
    <result>/index.jsp</result>
</action>
<action name="MyAction" class="your.fancy.app.MyAction">
    <interceptor-ref name="defaultStack"/>
    <interceptor-ref name="cookieProvider"/>
    <result>/index.jsp</result>
</action>

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 设置了域,则在测试此设置时,请确保您请求的是该域下的 URL。就我而言,我没有意识到我是直接访问我的测试机器而不是通过域,并且没有设置 cookie。