gpt4 book ai didi

java - 在 中使用
几次

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:08 27 4
gpt4 key购买 nike

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

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

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

我对 Spring 和 web 编程完全是个菜鸟。我将不胜感激。

最佳答案

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

关于java - 在 <foreach> 中使用 <form> 几次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16774850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com