Hidden field in spring MVC

有些话、适合烂在心里 提交于 2020-02-17 13:34:18

问题


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.



来源:https://stackoverflow.com/questions/7608027/hidden-field-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!