<i id='acd6q'><tr id='acd6q'><dt id='acd6q'><q id='acd6q'><span id='acd6q'><b id='acd6q'><form id='acd6q'><ins id='acd6q'></ins><ul id='acd6q'></ul><sub id='acd6q'></sub></form><legend id='acd6q'></legend><bdo id='acd6q'><pre id='acd6q'><center id='acd6q'></center></pre></bdo></b><th id='acd6q'></th></span></q></dt></tr></i><div id='acd6q'><tfoot id='acd6q'></tfoot><dl id='acd6q'><fieldset id='acd6q'></fieldset></dl></div>

<small id='acd6q'></small><noframes id='acd6q'>

  • <legend id='acd6q'><style id='acd6q'><dir id='acd6q'><q id='acd6q'></q></dir></style></legend>
  • <tfoot id='acd6q'></tfoot>

          <bdo id='acd6q'></bdo><ul id='acd6q'></ul>
      1. 在 Struts 2 和 Struts 中使用 cookie

        时间:2023-09-25
      2. <small id='CvtKh'></small><noframes id='CvtKh'>

          <tbody id='CvtKh'></tbody>
      3. <tfoot id='CvtKh'></tfoot>

            <bdo id='CvtKh'></bdo><ul id='CvtKh'></ul>
            <i id='CvtKh'><tr id='CvtKh'><dt id='CvtKh'><q id='CvtKh'><span id='CvtKh'><b id='CvtKh'><form id='CvtKh'><ins id='CvtKh'></ins><ul id='CvtKh'></ul><sub id='CvtKh'></sub></form><legend id='CvtKh'></legend><bdo id='CvtKh'><pre id='CvtKh'><center id='CvtKh'></center></pre></bdo></b><th id='CvtKh'></th></span></q></dt></tr></i><div id='CvtKh'><tfoot id='CvtKh'></tfoot><dl id='CvtKh'><fieldset id='CvtKh'></fieldset></dl></div>
            <legend id='CvtKh'><style id='CvtKh'><dir id='CvtKh'><q id='CvtKh'></q></dir></style></legend>

                  本文介绍了在 Struts 2 和 Struts 中使用 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下(缩短的)struts2 动作:

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

                  当我执行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:

                  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.

                  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从 jsp 将日期输入到 Struts2 动作? 下一篇:严重:异常启动过滤器 struts2 java.lang.ClassNotFoundException:org.apac

                  相关文章

                    <bdo id='iAj3a'></bdo><ul id='iAj3a'></ul>
                • <small id='iAj3a'></small><noframes id='iAj3a'>

                    <legend id='iAj3a'><style id='iAj3a'><dir id='iAj3a'><q id='iAj3a'></q></dir></style></legend>

                    <tfoot id='iAj3a'></tfoot>
                    1. <i id='iAj3a'><tr id='iAj3a'><dt id='iAj3a'><q id='iAj3a'><span id='iAj3a'><b id='iAj3a'><form id='iAj3a'><ins id='iAj3a'></ins><ul id='iAj3a'></ul><sub id='iAj3a'></sub></form><legend id='iAj3a'></legend><bdo id='iAj3a'><pre id='iAj3a'><center id='iAj3a'></center></pre></bdo></b><th id='iAj3a'></th></span></q></dt></tr></i><div id='iAj3a'><tfoot id='iAj3a'></tfoot><dl id='iAj3a'><fieldset id='iAj3a'></fieldset></dl></div>