Fill a form with saved cookies

拜拜、爱过 提交于 2019-11-29 08:58:22

To send cookie you can use a cookie-provider interceptor. It allows you to populate cookies in the action via implementing CookieProvider. To apply this interceptor to the action configuration you can override the interceptors config

<action ... >
  <interceptor-ref name="defaultStack"/>
  <interceptor-ref name="cookieProvider"/>
  ...
</action> 

The CookieProvider has a method to implement,

public class MyAction extends ActionSupport implements CookieProvider {

    @Override
    public Set<Cookie> getCookies(){
      Set<Cookie> cookies = new HashSet<>();
      Cookie name = new Cookie("name", userInfo.getName() );
      name.setMaxAge(60*60*24*365); // Make the cookie last a year!
      name.setPath("/"); //Make it at root.
      cookies.add(name);
      return cookies;
    }

}

In the form

<s:set var="name">${cookie["name"].value}</s:set>
<s:textfield
        label="Name"
        name="name"
        value="%{#name}"
        tooltip="Enter your Name here"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!