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;
and
public class FormItem { private String val1; private Integer val2;
And the controller method:
@RequestMapping() public String default(@ModelAttribute("formBean") FormBean formBean) {
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) {
source share