Ido Barash
Ido Barash

Reputation: 5142

Spring mvc url issues

I am using Spring mvc and I have this problem:

Every method in my controller returns a name of a jsp, and as for presentation - all good. The problem is with links (hrefs). The window's link does not change and the relative links gets me to unwanted places.

For example (not real):

I have a view which I can access by:/test1/{id} and an update view which I access by /test1/{id}/update

The post method, saves, and return the first view (getting you back to the viewing screen). The presentation is OK and I can see the updated data. But the window url was not changes and if I try to update again, I am sent to this location: /test1/{id}/update/update.

How can I solve this?

Upvotes: 1

Views: 430

Answers (1)

madhead
madhead

Reputation: 33501

Short answer: use redirects!

You MUST use redirects while doing something persistant. I guess you are modifying data when POSTing to those url. So you absolutely need to use redirects. This resolves the problem, when user refreshes the page after making an update. Have you ever noticed browser's warnings about resending the data?

Sample code:

@RequestMapping("/test1/{id}/update")
public ModelAndView update(@PathVariable("id") String id){
    // make an update
    return new ModelAndView("redirect:/test1/" + id);
}

This will send 301 or 302 HTTP status to the user. And the browser will imeddiately follow to the redirect url. Note that you should use not a view name, but url.

Upvotes: 1

Related Questions