在春天mvc 3,如何写一个cookie,而返回一个ModelAndView? [英] In spring mvc 3, how to write a cookie while returning a ModelAndView?
本文介绍了在春天mvc 3,如何写一个cookie,而返回一个ModelAndView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在方法中,它返回一个ModelAndView,但也有一个要求将cookie写回客户端。是可能在春天做它吗?谢谢。
In the method, it 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.
推荐答案
如果您将响应作为参数添加到处理程序方法中(请参阅 @RequestMapping注释方法的灵活签名 - 3.2.x , 4.0.x , 4.1.x ),您可以添加Cookie 到响应直接:
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), you may add the cookie to the response directly:
@RequestMapping("/example")
private ModelAndView exampleHandler(HttpServletResponse response) {
response.addCookie(new Cookie("COOKIENAME", "The cookie's value"));
return new ModelAndView("viewname");
}
这篇关于在春天mvc 3,如何写一个cookie,而返回一个ModelAndView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文