Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-MVC controller redirect to "previous" page?

Let's say I've got a form for editing the properties of a Pony, and in my web application there are multiple places where you can choose to edit a Pony. For instance, in a list of Ponies there might be an "edit" link next to each Pony, and when the user is viewing a Pony, there might also be an "edit" link in that view.

When a user clicks "submit" after editing a Pony, I would like to return the user to the page that he or she was when clicking the "edit" link.

How do I write my controller to redirect the user back to where they started? Certainly I can do this by passing a parameter to the controller, but that seems a little goofy. Am I thinking about this all wrong or is that pretty much what I'll have to do?

like image 823
Boden Avatar asked Apr 29 '09 22:04

Boden


People also ask

How use redirect attribute in Spring MVC?

Method SummaryAdd the supplied attribute to this Map using a generated name . Add the supplied attribute under the supplied name. Add the given flash storage using a generated name . Add the given flash attribute.

What is the difference between forward and redirect in Spring MVC?

Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.


1 Answers

Here's how to do it boys (Note this is RESTful Spring 3 MVC syntax but it will work in older Spring controllers):

@RequestMapping(value = "/rate", method = RequestMethod.POST) public String rateHandler(HttpServletRequest request) {     //your controller code     String referer = request.getHeader("Referer");     return "redirect:"+ referer; } 
like image 112
Sam Brodkin Avatar answered Sep 19 '22 01:09

Sam Brodkin