user520288
user520288

Reputation:

Spring MVC form with foreach

I have a question about Spring, particularly the MVC component. I have a jsp page which contains the following code.

    <form:form modelAttribute="sentenceModelAttribute"
           method="POST" action="sentencemanagement.htm">
       <table class="activity">                
          <tr>
             <th/>
             <th>ID</th>
             <th>Description</th>
             <th>Action</th>
             <th>Decision</th>
          </tr>

          <c:forEach items = "${model.allSentences}" var="sentence">
          <tr>
             <td><form:radiobutton path="id" value="${sentence.id}"/></td>
             <td>${sentence.id}</td>
             <td>${sentence.description}</td>
             <td>${sentence.action}</td>
             <td>${sentence.decision}</td>
          </tr>
          </c:forEach>

       </table>
       <input type="submit" name="modify" value="Modify"/>  
       <input type="submit" name="cancel" value="Cancel"/>            
    </form:form>

The POST is connected to a method in a controller and this method has an argument sentenceModelAttribute. Currently, this argument has all the values of the object from the selected radiobutton. This is indeed what I want. My question is how does it do that? How does it link the object I selected from the table with what appears in modelAttribute?

Upvotes: 1

Views: 10177

Answers (2)

WilQu
WilQu

Reputation: 7383

Your radiobutton has id as the path attribute. This path is relative to sentenceModelAttribute which is set in your form tag, so the value of the radiobutton is bound to sentenceModelAttribute.id.

Upvotes: 1

vacuum
vacuum

Reputation: 2273

It is done with DataBinder.

In case of Form POST, it works with WebDataBinder

Upvotes: 1

Related Questions