Reputation: 1245
In Spring MVC,
When the return value contains redirect:
prefix, the viewResolver
recognizes this as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL
. And the client will send a new request to this redirect URL.
We can write a handler method like this to handle the redirect:
@RequestMapping(value="/foo", method = RequestMethod.POST )
public String foo(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", "I am message");
return "redirect:/bar";
}
Now we can access this redirectAttribute in bar()
like this
@RequestMapping(value="/bar", method = RequestMethod.GET )
public String bar(HttpServletRequest request, HttpServletResponse response, Model model) {
String error = (String) model.asMap().get("message");
}
Normally we can access this the redirectAttribute inside bar()
method, But when I specify a URL as parameterised url in return statement like this below
return "redirect:/bar?x=1&y=2";
I am unable to access the redirectAttributes
I further Inspected network in chrome and I found that while using un-parameterised url in return statement jsessionid
remains same after redirect, but it does changes while using parameterised urls.
Can anyone please tell me why is this happening or am I going wrong somewhere?
Upvotes: 2
Views: 744
Reputation: 4084
If you want to access the value of paramater x
and y
in your redirect controller you need to get Parameter
from request
.
@RequestMapping(value="/bar", method = RequestMethod.GET )
public String bar(HttpServletRequest request, HttpServletResponse response, Model model) {
System.out.println(request.getParameter("y"));
return null;
}
Upvotes: 1