Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Spring MVC controller return both a HttpServletResponse and a view?

My existing code is like:

String myController(@PathVariable someId, ModelMap map){
....
return "myViewName";
}

Now I want to set a cookie in some cases, so I need to get hold of a HttpServletResponse obj. Can I just add such a response obj to the list of params and operate on it in the controller? If so, I wonder how my own response is kind of reconciled with the response generated by the JSP that resolves the "myViewName".

like image 594
teddy teddy Avatar asked Oct 31 '13 01:10

teddy teddy


1 Answers

Yes.

@RequestMapping
public String myController(@PathVariable someId, ModelMap map, HttpServletResponse response) {
    // Do what you need to do on the response, like set a cookie
    return "myViewName";
}
like image 185
MattSenter Avatar answered Nov 15 '22 03:11

MattSenter