在 Struts 2 和 Struts 中使用 cookie

发布于 2024-09-11 13:00:21 字数 601 浏览 3 评论 0原文

我有以下(缩短的)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哥,最终变帅啦 2024-09-18 13:00:21

看来struts只支持读取cookie,您必须转到servlet响应才能实际设置 cookie。

最后,我选择完全绕过 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;
  }
}

并且此方法不需要在 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:

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.

凉风有信 2024-09-18 13:00:21

您还需要为您的操作定义实现 Cookie 拦截器 struts.xml:

<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>

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

<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>
温柔少女心 2024-09-18 13:00:21

虽然我知道这个问题已经存在了 3 年多了,但今天我需要使用 Struts2 设置 cookie,来到这里,并设法以 Struts2-y 方式设置 cookie(使用 2.3.16)。希望这对其他人有帮助。

为了使用 Struts2 设置 cookie,您需要执行以下步骤:

  1. 让您的 Action 实现org.apache.struts2.interceptor.CookieProvider。 (您可能想查看其 javadoc)
  2. 实现 SetgetCookies();方法,返回所有你想要设置的cookie。
  3. 让您的 Action 使用 cookieProvider 拦截器与@Pat 在他的回答中提到的方式相同。
<操作名称=“MyAction”类=“your.fancy.app.MyAction”>
    
    >
    <结果>/index.jsp

如果您为 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:

  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 cookieProvider interceptor the same way as @Pat mentioned in his answer.
<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文