Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In spring mvc 3, how to write a cookie while returning a ModelAndView?

My controller method is returning a ModelAndView, but there is also a requirement to write a cookie back to client. Is it possible to do it in Spring? Thanks.

like image 915
Bobo Avatar asked Feb 03 '11 16:02

Bobo


People also ask

How do you get cookies in the spring?

To set a cookie in Spring Boot, we can use HttpServletResponse class's method addCookie() . All you need to do is to create a new instance of Cookie class and add it to the response.

Which annotation is used to set cookies?

Annotation Type CookieValue Annotation to indicate that a method parameter is bound to an HTTP cookie. The method parameter may be declared as type Cookie or as cookie value type (String, int, etc.). Note that with spring-webmvc 5.3. x and earlier, the cookie value is URL decoded.

What does ModelAndView return?

ModelAndView is a holder for both Model and View in the web MVC framework. These two classes are distinct; ModelAndView merely holds both to make it possible for a controller to return both model and view in a single return value. The view is resolved by a ViewResolver object; the model is data stored in a Map .


3 Answers

If you add the response as parameter to your handler method (see flexible signatures of @RequestMapping annotated methods – same section for 3.2.x, 4.0.x, 4.1.x, 4.3.x, 5.x.x), you may add the cookie to the response directly:

Kotlin

@RequestMapping(["/example"]) fun exampleHandler(response: HttpServletResponse): ModelAndView {    response.addCookie(Cookie("COOKIENAME", "The cookie's value"))    return ModelAndView("viewname") } 

Java

@RequestMapping("/example") private ModelAndView exampleHandler(HttpServletResponse response) {          response.addCookie(new Cookie("COOKIENAME", "The cookie's value"));          return new ModelAndView("viewname"); } 
like image 144
Wolfram Avatar answered Nov 13 '22 04:11

Wolfram


Not as part of the ModelAndView, no, but you can add the cookie directly to the HttpServletResponse object that's passed in to your controller method.

like image 44
skaffman Avatar answered Nov 13 '22 04:11

skaffman


You can write a HandlerInterceptor that will take all Cookie instances from your model and generate the appropriate cookie headers. This way you can keep your controllers clean and free from HttpServletResponse.

@Component
public class ModelCookieInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            for (Object value : modelAndView.getModel().values()) {
                if (value instanceof Cookie)
                    res.addCookie((Cookie) value);
            }
        }
    }

}

NB . Don't forget to register the interceptor either with <mvc:interceptors> (XML config) or WebMvcConfigurer.addInterceptors() (Java config).

like image 21
rustyx Avatar answered Nov 13 '22 03:11

rustyx