使用Cookie将值保存在Spring MVC表单字段中

时间:2012-11-01 08:44:01

标签: java spring jsp spring-mvc cookies

假设我有一个 Spring MVC 形式:

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

我要做的是将这些字段值存储在Cookie中,以便在用户登录系统时保存这些值。它类似于Remember Me复选框,但没有复选框。我的控制器看起来像这样:

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

但是当我单击按钮并调用此方法时,我的值不会保存。这是我第一次尝试Cookies,所以也许我错过了什么?我正在关注this SO问题。但就我而言,这不起作用。任何人都可以建议我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

抱歉是误报。我正在做的一切正确,但我有一个方法,在每个请求中返回新的AuthenticationForm对象作为模型属性,如下所示:

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

我有一个方法用于在视图中显示表单:

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

然后我意识到这个表单对象总是新的,所以我从cookie设置的值总是在新表单上。我必须在showForm方法中设置cookie的值,一切正常。

答案 1 :(得分:0)

请检查这是否有用。 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");
}