Paulius Matulionis
Paulius Matulionis

Reputation: 23415

Using Cookies to save the values in Spring MVC form fields

Lets say I have a Spring MVC form like this:

<form:form action="${pageContext.servletContext.contextPath}/secure/main.htm" commandName="secure/main">

    <form:input path="operatorId" cssClass="textField"/>

    <form:input path="clientId" cssClass="textField"/>

</form:form>

What I am trying to do is to store those fields values in the cookies that they will be saved other time user logins into the system. It is similar to Remember Me checkbox, but just without the checkbox. My controller looks like this:

@RequestMapping(method = RequestMethod.POST)
public String processAuthenticate(@Valid AuthenticationForm authenticationForm,
                                  Map<String, Object> model,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

    authenticationForm = (AuthenticationForm) model.get("authenticationForm");

    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
            authenticationForm.setClientId(cookie.getValue());
        } else if (cookie.getName().equals("operatorId")) {
            authenticationForm.setOperatorId(cookie.getValue());
        }
    }

    String clientId = authenticationForm.getClientId();
    String operatorId = authenticationForm.getOperatorId();

    Cookie cookieClientId= new Cookie("clientId", clientId);
    cookieClientId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieClientId);

    Cookie cookieOperatorId = new Cookie("operatorId", operatorId);
    cookieOperatorId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieOperatorId);

    return MAIN_FORM_MAPPING;
}

But when I click the button and this method is invoked, my values are not saved. It is the first time I am trying to use Cookies so maybe I am missing something? I was following this SO question. But in my case this does not work. Anybody could advice me with the solution to this problem?

Upvotes: 3

Views: 13036

Answers (2)

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

Sorry for the false alarm. I was doing everything right, but I had a method which within each request returned new AuthenticationForm object as a model attribute, like this:

@ModelAttribute("secure/" + MAIN_FORM_MAPPING)
public AuthenticationForm getAuthenticationForm() {
    return new AuthenticationForm();
}

And I had a method which is used to show the form in the view:

@RequestMapping(method = {RequestMethod.GET})
public String showForm(Map<String, Object> model, HttpServletRequest request) {
    AuthenticationForm authenticationForm = (AuthenticationForm) model.get("secure/main");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
           authenticationForm.setClientId(cookie.getValue());   //Just added this code to this method
        } else if (cookie.getName().equals("operatorId")) {
           authenticationForm.setOperatorId(cookie.getValue());
        }
    }
    return MAIN_FORM_MAPPING;
}

Then I realized that this form object is always new, so the values I am setting from cookies always on a new form. I had to set the values from cookies in the showForm method and everything is working.

Upvotes: 2

Sajith
Sajith

Reputation: 2036

Please check whether this is useful. In spring mvc 3, how to write a cookie while returning a ModelAndView?

@RequestMapping("/example")
private ModelAndView exampleHandler(HttpServletResponse response) {

        response.addCookie(new Cookie("COOKIENAME", "The cookie's value"));

        return new ModelAndView("viewname");
}

Upvotes: 0

Related Questions