次のような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の質問に従っていました。しかし、私の場合、これは機能しません。この問題の解決策を教えてくれる人はいますか?