Using cookies with Struts 2 and Struts

后端 未结 4 821
囚心锁ツ
囚心锁ツ 2020-12-15 10:41

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

public class MyAction extends BaseAction implements CookiesAware {

  public String execute() {

    if (         


        
相关标签:
4条回答
  • 2020-12-15 10:59

    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>
    
    0 讨论(0)
  • 2020-12-15 11:01

    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.

    0 讨论(0)
  • 2020-12-15 11:01

    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.

    0 讨论(0)
  • 2020-12-15 11:14

    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

    0 讨论(0)
提交回复
热议问题