3

假设我有一个这样的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所以也许我错过了什么?我一直在关注这个 SO question。但就我而言,这不起作用。有人可以建议我解决这个问题吗?

4

2 回答 2

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

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

于 2012-11-01T09:26:08.203 回答
0

请检查这是否有用。 在spring mvc 3中,如何在返回ModelAndView的同时编写cookie?

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

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

        return new ModelAndView("viewname");
}
于 2012-11-01T08:52:44.377 回答