假设我有一个这样的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。但就我而言,这不起作用。有人可以建议我解决这个问题吗?