Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would achieve that by having another POJO serving as a container with a list of your POJOs inside.</p> <p>This would look like this :</p> <pre><code>public class PojoForm { private List&lt;Pojo&gt; pojos; public List&lt;Pojo&gt; getPojos() { return pojos; } public void setPojos(List&lt;Pojo&gt; pojos) { this.pojos = pojos; } } </code></pre> <p>Then in the controller, use this container instead of the actual pojo as a model attribute.</p> <pre><code>@ModelAttribute("pojoForm") public PojoForm populatePojos() { // Don't forget to initialize the pojos list or else it won't work PojoForm pojoForm = new PojoForm(); List&lt;Pojo&gt; pojos = new ArrayList&lt;Pojo&gt;(); for(int i=0; i&lt;2; i++) { pojos.add(new Pojo()); } pojoForm.setPojos(pojos); return pojoForm; } @RequestMapping(method=RequestMethod.POST) public String saveForm(@ModelAttribute("pojoForm") PojoForm pojoForm) { for(Pojo pojo : pojoForm.getPojos()) { service.save(pojo); } return "theview.jsp"; } </code></pre> <p>Then the view should look something like this :</p> <pre><code>&lt;form:form commandName="pojoForm" method="POST"&gt; &lt;!-- Pojo 1 --&gt; &lt;form:input path="pojos[0].a" /&gt; &lt;form:input path="pojos[0].b" /&gt; &lt;form:input path="pojos[0].c" /&gt; &lt;!-- Pojo 2 --&gt; &lt;form:input path="pojos[1].a" /&gt; &lt;form:input path="pojos[1].b" /&gt; &lt;form:input path="pojos[1].c" /&gt; &lt;/form:form&gt; </code></pre> <p>a, b and c being the properties of the Pojo class.</p> <p>You can also directly loop on the list like this :</p> <pre><code>&lt;form:form commandName="pojoForm" method="POST"&gt; &lt;c:forEach items="${pojoForm.pojos}" varStatus="i"&gt; &lt;form:input path="pojos[${i.index}].a" /&gt; &lt;form:input path="pojos[${i.index}].b" /&gt; &lt;form:input path="pojos[${i.index}].c" /&gt; &lt;/c:forEach&gt; &lt;/form:form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload