Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

16

Feb

Fixing Spring’s Default View Resolver

Most Spring MVC applications use org.springframework.web.servlet.view.UrlBasedViewResolver

This view resolver allows you to redirect with the prefix “redirect:” thus you can do something like:

	@RequestMapping(value={"/doc/api", "/doc/api/"})
	public String apiDoc(ModelMap model) {
		//some logic
		m.put("hiddenStuff", "blah blah");
		if (success) { return "myview"; }
                else { return "redirect:/doc/api/index.html"; }
	}

This will redirect to http://host/doc/api/index.html?hiddenStuff=blah%20blah . This is probably not what you expected and also seems rather dangerous that its attaching model attributes as parameters.

Below is a fix and some enhancements for this annoying problem:

This view resolver allows you to redirect with out attaching the model attributes with redirect-no-model:. I have also added a way to do a permanent redirect (301) with redirect-permanently: instead of 307. You would use this if you refactored a public URL for SEO purposes.

  1. agentgt posted this