java 在 <foreach> 中使用 <form> 几次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16774850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:59:12  来源:igfitidea点击:

using <form> within <foreach> a few time

javaspringspring-mvcjstl

提问by Andrey Yaskulsky

I'm pretty new to Spring and I'm trying to use Spring MVC + JSP + JSTL. My goal is to make JSP which containts list of Users and allow to edit each User separately from others. So I think I should use separate <form>tag and separate <sumbit>button for every User in the list and my JSP looks like:

我对 Spring 很陌生,我正在尝试使用 Spring MVC + JSP + JSTL。我的目标是制作包含用户列表的 JSP,并允许将每个用户与其他用户分开编辑。所以我想我应该为列表中的每个用户使用单独的<form>标签和单独的<sumbit>按钮,我的 JSP 看起来像:

<c:forEach items="${userList}" var="currentUser" varStatus="index">
        <form:form method="post" action = "edit" commandName="userList[${index}]">
        <tr>
            <td><form:input path = "userList[${index}].login" value = "${currentUser.login}" /></td>
            <td><form:input path = "userList[${index}].password" value = "${currentUser.password}" /></td>
            <td><form:input path = "userList[${index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
            <td><form:input path = "userList[${index}].popServer" value = "${currentUser.popServer}" /></td>
                <form:hidden path="userList[${index}].id" value=""/>
            <td>
                <a href="delete/${user.id}"><spring:message code="label.delete" /></a>
            </td>
        </tr>
           <input type="submit" value = "edit"> 
        </form:form>
    </c:forEach>

the idea is to have an opportunity to edit each user separately by pressing the button "edit". Ofcourse this code doesn't work. It gives me an exception:

这个想法是有机会通过按下“编辑”按钮来分别编辑每个用户。当然,这段代码不起作用。它给了我一个例外:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userList[javax' available as request attribute

java.lang.IllegalStateException:对于 bean 名称“userList[javax”既不是 BindingResult 也不是普通目标对象作为请求属性可用

I'm really totally noob at Spring and at web-programming too. I'll appreciate any help.

我对 Spring 和 Web 编程也完全是个菜鸟。我将不胜感激任何帮助。

采纳答案by storm_buster

I dont understant why you are using userList[${index}]since you have a different form for each user. Anyway your code isnt right at this line commandName="userList[${index}]"

我不明白您为什么使用,userList[${index}]因为每个用户都有不同的表单。无论如何,您的代码不在这一行commandName="userList[${index}]"

Here is what I suggest :

这是我的建议:

<c:forEach items="${userList}" var="currentUser" varStatus="index">
    <form:form method="post" action = "edit" commandName="user">
    <tr>
        <td><form:input path = "login" value = "${currentUser.login}" /></td>
        <td><form:input path = "password" value = "${currentUser.password}" /></td>
        <td><form:input path = "smtpServer" value = "${currentUser.smtpServer}" /></td>
        <td><form:input path = "popServer" value = "${currentUser.popServer}" /></td>
            <form:hidden path="id" value=""/>
        <td>
            <a href="delete/${user.id}"><spring:message code="label.delete" /></a>
        </td>
    </tr>
       <input type="submit" value = "edit"> 
    </form:form>
</c:forEach>

According that your user class is user.

根据您的用户类别是用户。

回答by BoatCode

Your use of ${index} isn't what you think. To get the index of the current item you must append ".index" to your varStatus variable... in your case it would be ${index.index}. For clarity, consider naming your varStatus something other than index...

您对 ${index} 的使用不是您所想的。要获取当前项目的索引,您必须将“.index”附加到您的 varStatus 变量......在您的情况下,它将是 ${index.index}。为清楚起见,请考虑将 varStatus 命名为 index 以外的其他名称...

<c:forEach items="${userList}" var="currentUser" varStatus="uStatus">
    <form:form method="post" action = "edit" commandName="userList">
    <tr>
        <td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
        <td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
        <td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
        <td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
            <form:hidden path="userList[${uStatus.index}].id" value=""/>
        <td>
            <a href="delete/${currentUser.id}"><spring:message code="label.delete" /></a>
        </td>
    </tr>
       <input type="submit" value = "edit"> 
    </form:form>
</c:forEach>

Keep in mind, this doesn't make sense using a different form for each user... but say you wanted to edit any/all of your users at once....

请记住,为每个用户使用不同的表单是没有意义的……但是假设您想一次编辑任何/所有用户……

<form:form method="post" action = "edit" commandName="userList">
    <c:forEach items="${userList}" var="currentUser" varStatus="uStatus">

    <tr>
        <td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
        <td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
        <td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
        <td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
            <form:hidden path="userList[${uStatus.index}].id" value=""/>
        <td>
            <a href="delete/${currentUser.id}"><spring:message code="label.delete" /></a>
        </td>
    </tr>

    </c:forEach>
    <input type="submit" value = "edit"> 
</form:form>