I’m displaying the view newMessage.jsp on /messages/new. Once the new message has been successfully sent, I want to redirect the user to the page where he came from. That could be a lot of pages, since I have my new message link embedded in a lot of my views (Inbox, Complaints page etc.).
Experiment #1
I tried return "redirect:" + request.getHeader("Referer"), but it doesn’t work. The reason is, each time the user clicks submit, I’m sending him to the same url (/messages/new) and hence the Referer Header gets set to /messages/new after he clicks on ‘Submit’
Experiment #2
Then I tried setting a @RequestParam (which seemed like the proper thing to do). So, I would call /messages/new?referer=/inbox in the Inbox view, /messages/new?referer=/complaints in the Complaints view, and so on. But again, when the user clicks ‘Submit’, action="new" is triggered and I lose the referer param I’d passed.
Experiment #3
Finally, I decided to edit the action="new" to action="new?referer=${referer}". This worked pretty fine, except that when referer was empty, the url would be /messages/new?referer=, which doesn’t look quite pretty.
Experiment #4
Even if I go with (3), when I finally (after successfully sending the message) do return "redirect:" + my_domain_name + referer ; in the Controller, the request is being passed with the old value of referer and the resulting URL is my_domain_name/referer?referer=value_from_previous_call while it should be just my_domain_name/referer
Well, the only apparent solution is:
do
return "redirect:" + referer ;from the controller and it will redirect the page to wherever/messages/newwas called from.