I wanted to use spring 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.
Thanks
< form:hidden path = "empId" /> This tag will generate an HTML hidden tag at the time of processing like below. HTML.
I think I solved the problem. If I write input tag like this
<form:hidden path="id" value="${record}" />
in this way I can reassign the value of hidden variable but when I look into the rendered html code its like this
<input type="hidden" value="0" name="record" value="10"/>
generate the value attribute twice and takes the value I wanted which is 10.But it solves my problem. If anyone has further comments on this then that will be appreciated.
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.
In the rest of this answer, substitute "delete" and "deteted" with the operation you are attempting to implement. for example "exploded", "bitten", or "edited"
The JSP code you posted has several issues.
<td>
element.getElementById()
call can never work.href="#"
will cause your page to scroll to the top when the user clicks the link.Here is what I think you want:
<c:forEach var="record" items="${records}">
<tr>
<td>
<form:form method="post">
<input type="hidden" name="activity" value="delete"/>
<input type="hidden" name="record" value="${record}"/>
<a href="javascript:this.form.submit()">Delete ${record}</a>
</form:form>
<td>
</tr>
</c:forEach>
The snippet will post to the current spring controller. Two fields are included in the post: "activity" which identifies this as a delete and "record" which identifies the record to be deleted. Based on your needs, add action="some url"
to the form:form
tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With