Reputation: 921
Spring redirect in controller not working like return "redirect:/reservation/reservationSuccess" but return "/reservation/reservationSuccess"; is working. Why it is not working. where it went wrong. Please help.
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute("reservation") Reservation reservation,
BindingResult result,
SessionStatus status,
Model model) {
validator.validate(reservation, result);
if(result.hasErrors()) {
model.addAttribute("reservation",reservation);
return "reservation/reservationForm";
}
reservationService.make(reservation);
status.setComplete();
return "redirect:reservation/reservationSuccess";
}
Upvotes: 2
Views: 15617
Reputation: 1791
"redirect:xxx" is looking for a RequestMapping to match the redirect string xxx, however, the return "xxx" is going to look for View Resolver to map that string to a JSP page. That is the main difference.
Upvotes: 0
Reputation: 1206
You have to have another method in your Controller to intercept the reservation/reservationSuccess
GET request.
For example:
@RequestMapping(value="reservation/reservationSuccess", method = RequestMethod.GET)
public String getSuccess() {
return "reservation/reservationForm";
}
Upvotes: 1
Reputation: 691735
When you're doing a redirect to reservation/reservationSuccess
, by definition, the browser will send a new request to the URL reservation/reservationSuccess
of your web app. You will see the complete URL in the address bar of your browser.
If this URL is not mapped to any servlet in your web app, you will obviously get a 404 error.
You need to understand that the point of a redirect is not to dispatch to a view (a JSP). The point is to make the browser go to another URL in your web app. The path you put after the redirect:
prefix is thus supposed to be the path of an action of your Spring MVC app. Not the path of a view.
Upvotes: 7