Hidden field in spring MVC

2020-05-23 12:04发布

问题:

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

回答1:

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.



回答2:

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.

  1. no close tag for the <td> element.
  2. your form is posting to "items.html". This seems to be an html page. If so, your form action is 0% correct.
  3. every form has the same id, so the getElementById() call can never work.
  4. href="#" will cause your page to scroll to the top when the user clicks the link.
  5. You do not identify, to the user, the record to be deleted.

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.



回答3:

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.