Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring controller get request/response

How do I get the request/response that I can setcookie? Additionally, at the end of this method, how can I can redirect to another page?

@RequestMapping(value = "/dosomething", method = RequestMethod.GET)
public RETURNREDIRECTOBJ dosomething() throws IOException {
    ....
    return returnredirectpagejsp;
}
like image 861
cometta Avatar asked Dec 30 '10 16:12

cometta


People also ask

How do you access HTTP request and response objects in controller methods?

To access HttpRequest from controller you just need to define HttpServletRequest and HttpServletResponse as parameters in a function signature of your Controller. by doing this you allowing Spring MVC to pass these objects to you controller method.

What is difference between @RequestBody and @ResponseBody?

By using @RequestBody annotation you will get your values mapped with the model you created in your system for handling any specific call. While by using @ResponseBody you can send anything back to the place from where the request was generated. Both things will be mapped easily without writing any custom parser etc.

What is @ResponseBody annotation in Spring?

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.


1 Answers

How about this:

@RequestMapping(value = "/dosomething", method = RequestMethod.GET)
public ModelAndView dosomething(HttpServletRequest request, HttpServletResponse response)  throws IOException {
    // setup your Cookie here
    response.setCookie(cookie)
    ModelAndView mav = new ModelAndView();
    mav.setViewName("redirect:/other-page");

    return mav;
}
like image 146
MatBanik Avatar answered Sep 25 '22 09:09

MatBanik