Issue
I wanted to use Spring MVC hidden tag in below code. Is this possible in below code, what I have to write in my controller to do that or what I am doing is correct.
<c:forEach var="record" items="${records}">
<tr>
<td>
<form:form id="myForm" action="list.html" method="post">
<input type="hidden" name="record" value="${record}" />
<a href="#" onclick="document.getElementById('myForm').submit();">Submit</a>
</form:form>
</td>
</tr>
</c:forEach>
Any help will be highly appriciated.
Solution
You are on the right track [depending on what your backing bean is], but in order to bind an ID as a hidden field on submission automatically to a "Person" bean (in this example), you would do something like:
<c:forEach var="person" items="${persons}" varStatus="status">
<tr>
<c:set var="personFormId" value="person${status.index}"/>
....
<form id="${personFormId}" action="${deleteUrl}" method="POST">
<input id="id" name="id" type="hidden" value="${person.id}"/>
</form>
<td>${person.firstName}</td>
<td>${person.lastName}</td>
....
</tr>
</c:forEach>
In case you'd like to render a hidden field, you would use a form:hidden
tag:
<form:hidden path="id" />
Take a look at Hidden Input Tag section of the Spring docs.
Answered By - tolitius
Answer Checked By - Gilberto Lyons (JavaFixing Admin)