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

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

次に、このフォームオブジェクトは常に新しいため、Cookieから設定する値は常に新しいフォームにあることに気付きました。メソッドで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 に答える