Using @RequestParam for multiple POST options

I have a very familiar problem solved in this thread: click

Shortly speaking:

I generate form elements dynamically using jQuery, and as a result, it looks like this:

<div id="XYZ-1"> <input type="text" id="XYZ-1-position" name="XYZ-1-position" style="width: 20px;"/> <input type="text" id="XYZ-1-input" name="XYZ-1-value" style="width: 400px;"/> </div> <div id="XYZ-2"> <input type="text" id="XYZ-2-position" name="XYZ-2-position" style="width: 20px;"/> <input type="text" id="XYZ-2-input" name="XYZ-2-value" style="width: 400px;"/> </div> 

So, to process a single form field, I use only this simple @ReqeustParam:

  @RequestMapping(value = "/data", method = RequestMethod.POST) public String myHandler(@RequestParam("XYZ-1-value")String content, Model model) { model.addAttribute("dataset", content); return "data" 

In the thread of the beginning of my message, it is solved as follows:

 public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){ .... } 

And this works for input types with the same name:

 <input type="checkbox" name="myParam[]" value="myVal1" /> <input type="checkbox" name="myParam[]" value="myVal2" /> 

But the difference in my problem is that my form elements do not have the same name - each has an individual name.

So my question is how can I handle these individual post parameters in one handler in my controller.

Thank you in advance

0
source share
2 answers

The sample that I like for this is to have a bean form that contains a list (or map) of beans that wraps my individual values. So, I would:

 public class FormBean { private List<FormItem> items; //...getters/setters } 

and

 public class FormItem { private String val1; private Integer val2; //...getters/setters } 

And the controller method:

 @RequestMapping() public String default(@ModelAttribute("formBean") FormBean formBean) { // Blah blah blah } 

On the view side, the display name will look like this:

 <input type="text" name="formBean.items[0].val1" /> <input type="text" name="formBean.items[0].val2" /> 

Or you can use JSTL tags:

 <form:form modelAttribute="formBean"> <form:input path="items[0].val1" /> <form:input path="items[0].val2" /> </form:form> 

Thus, everything is beautiful and packed, and on the viewing side it is very simple to parse the name when adding new lines.

EDIT

A simple example of automatic installation of models in a session (I am typing this from memory ... please confirm it yourself)

Controller:

 @Controller @RequestMapping("/form") @SessionAttributes({"formBean"}) public class FormController { @RequestMapping() public String defaultView(@ModelAttribute("formBean") FormBean formBean, Model uiModel) { // Do something...add anything to the uiModel that you want to use in your form...you should not need to add formBean as it should auto-matically get put into session now with the @SessionAttributes annotation // Also to note, if you set the "formBean" attribute on the Model or ModelAndView, it will replace the one in Session. Also, you can add the SessionStatus to one of your methods and use its .setComplete() method to indicate you are done with the session...you usually do this after you know the user is done with the application so the session can get cleaned up. } @RequestMapping("/someOtherPath") public String someOtherHandler(@ModelAttribute("formBean") FormBean formBean, Model uiModel) { // Do something...again, formBean should be either created or out of session } @ModelAttribute("formBean") private FormBean createFormBean() { // This method can be named anything, as long as it returns a FormBean, has the @ModelAttribute named on it, and has no arguments. Use this method to populate your FormBean when its created (set defaults and such). return new FormBean(); } } 
+1
source

Firstly, you do not need [] - it will work without it. But if the name of each flag is different, then a separate @RequestParam is required for each flag.

0
source

All Articles