views:

4252

answers:

3

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?

+2  A: 

One option, of course, would be to open the edit form in a new window, so all the user has to do is close it and they're back where they were.

There are a few places in my current application where I need to do something complicated, then pass the user to a form, and then have them return to the starting point. In those cases I store the starting point in the session before passing them off. That's probably overkill for what you're doing.

Other options: 1) you can store the "Referer" header and use that, but that may not be dependable; not all browsers set that header. 2) you could have javascript on the confirmation page after the form submission that calls "history.go(-2)".

JacobM
A: 

Yes I think Jacob's idea for the form in a new window may be a good option. Or in a hidden div. Like a dojo dialog. http://dojocampus.org/explorer/#Dijit_Dialog_Basic

Derek
+2  A: 

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;
}
Sam Brodkin