Spring 3 MVC - how to turn a form into a query string?

I have a simple Spring form that is bound to a form object in a post. The http POST handler does some work, and then it needs to be redirected to a new URL, passing the form data as request parameters.

So, if I have a form support object with the properties "param1" and "param2", I want to build a line that looks something like this:

redirect:/app/new/page?param1=value;param2=value

Spring will now automatically bind FROM values ​​to a querystring or form message in my form object, but I want GENERATE to issue a request with values ​​taken from the form object.

Obviously, it is trivial to do this manually, but since I will have many different form support objects, is there any built-in tool in Spring to create a query string from a form object suitable for building in the URL?

Thanks.

+6
java spring-mvc
source share
3 answers

OK, after some tracing in the Spring source code, I think I have some final answers.

1) Manually adding request parameters to the URL that you pass back to the string "redirect: ..." is a VERY BAD IDEA. Spring caches the views referenced by these URLs, so if you specify many different parameters, you are effectively leaking memory, forcing the cache to store many values ​​that it does not need.

Please note that the PetClinic example code in the Spring distribution does just that, and apparently the Jira problem has been raised for fixing :-)

2) The model object is still held by Spring when the controller returns, and Spring will automatically add values ​​to it at the URL you specify (as request parameters) - it PROVIDES that the model values ​​are equal to String or primitive objects. Now, since the object of the form support is an object stored as a single attribute, Spring does nothing with it.

So - from an annotated controller, the best approach seems to be to add the ModelMap parameter to the handler method. When you are ready to return, do something like this:

 Assuming the form backing object "formObject" has been passed as a parameter to the controller handler method (via the ModelAttribute annotation) and has properties "param1" and "param2" that are Strings - modelMap.clear(); modelMap.addAttribute("param1", formObject.getParam1()); modelMap.addAttribute("param2", formObject.getParam2()); return "redirect:/my/url"; 

And will Spring issue a redirect to / my / url? param1 = value; param2 = value

It seems that there is no built-in mechanism (in Spring) to turn the bean into a list of key / value pairs to add to the map, but if you really need it, the Apache BeanUtils Library will be fine. Of course, you do not want to do this for large objects, otherwise you may exceed the permissible length of the URL.

Finding all this turned out to be much more difficult than necessary :-)

+2
source share
 Map<String, Object> model = new HashMap<String, Object(); model.put("stuff", "here"); ModelAndView mv = new ModelAndView("redirect:/somewhere.htm", model); return mv; 

Gives out

 http://.../somewhere.htm?stuff=here 
+2
source share

I think you should use the setExposeModelAttributes method of the RedirectView class, but it can be difficult to access this method if you use Spring 3.0 annotations on controllers.

0
source share

All Articles