4

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

<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:Bean 名称“userList [javax”的 BindingResult 和普通目标对象都不能用作请求属性

在 Spring 和网络编程方面,我真的完全是菜鸟。我会很感激任何帮助。

4

2 回答 2

5

您对 ${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>

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

<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>
于 2014-04-09T14:44:28.660 回答
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>

根据您的用户类是用户。

于 2013-05-27T14:07:33.057 回答