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

时间:2013-05-27 13:59:14

标签: java spring spring-mvc jstl

我是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>

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

  

java.lang.IllegalStateException:既不是BindingResult也不是plain   bean名称'userList [javax']的目标对象可用作请求   属性

我真的非常喜欢Spring和网络编程。我会感激任何帮助。

2 个答案:

答案 0 :(得分:5)

您使用$ {index}并不是您的想法。要获取当前项目的索引,您必须附加&#34; .index&#34;你的varStatus变量...在你的情况下它将是$ {index.index}。为清楚起见,请考虑将varStatus命名为索引以外的其他内容...

<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>

请注意,对于每个用户使用不同的表单并不合理......但是说您想要一次编辑任何/所有用户....

<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>

答案 1 :(得分:1)

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

以下是我的建议:

<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>

根据您的用户类是用户。