java 使用 Spring 的表单标签动态绑定列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1934129/
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
Dynamically binding lists with Spring's form tag
提问by ravun
I have a command object FaxFormand it holds a list of FaxStatusobjects inside a faxStatusListproperty.
我有一个命令对象FaxForm,它包含FaxStatus一个faxStatusList属性内的对象列表。
public class FaxForm {
private List<FaxStatus> faxStatusList;
public void setFaxStatusList(List<FaxStatus> faxStatusList) {
this.faxStatusList = faxStatusList;
}
public List<FaxStatus> getFaxStatusList() {
return faxStatusList;
}
}
I initially had a JSP page that would bind the objects by performing the following:
我最初有一个 JSP 页面,它将通过执行以下操作来绑定对象:
<c:forEach items="${esaFaxForm.faxStatusList}" var="item" varStatus="loop">
<tr class="tableAltBackground">
<td>
<form:checkbox path="faxStatusList[${loop.index}].selected"/>
</td>
<td>
<form:select path="faxStatusList[${loop.index}].status" items="${esaFaxForm.statusOptions}" onchange="checkThisBox(this);"/>
</td>
<td>
<a href="${statusContUrl}?id=${item.id}&status=${item.status}" onclick="openFaxWindow('${viewFaxUrl}?id=${item.id}', ${loop.index});">${item.name}</a>
<form:hidden path="faxStatusList[${loop.index}].name"/>
</td>
<td>
<a href="${statusContUrl}?id=${item.id}&status=${item.status}" onclick="openFaxWindow('${viewFaxUrl}?id=${item.id}', ${loop.index});">${item.id}</a>
<form:hidden path="faxStatusList[${loop.index}].id"/>
</td>
</tr>
</c:forEach>
However, I am trying to figure out how I could do the binding without the forEach loop and index. The examples on the Springwebsite show the binding by setting the path to the list name. Is there a way to bind the properties? I've tried this but it fails:
但是,我试图弄清楚如何在没有 forEach 循环和索引的情况下进行绑定。Spring网站上的示例通过设置列表名称的路径来显示绑定。有没有办法绑定属性?我试过这个,但它失败了:
<form:checkbox path="faxStatusList.faxStatus.selected"/>
<form:select path="faxStatusList.faxStatus.status" items="${esaFaxForm.statusOptions}"/>
The faxStatusList has a getter and setter method and the FaxStatus variables each have getter/setter properties. I get the error "Invalid property 'faxStatusList.faxStatus' of bean class..."
faxStatusList 有一个 getter 和 setter 方法,FaxStatus 变量每个都有 getter/setter 属性。我收到错误“bean 类的无效属性 'faxStatusList.faxStatus'...”
采纳答案by Sasi
Spring form tags has a checkboxestag. You can use it as follows to do the binding automatically:
Spring 表单标签有一个复选框标签。您可以按如下方式使用它来自动进行绑定:
<form:checkboxes items="${faxStatusList}" path="faxStatusList" itemLabel="name" itemValue="id" delimiter="<br/>" onclick="yourOnClickMethodIfYouNeed(this);"/>
The above snippet will display a list of checkbox items delimited with the br tag. Any changes made to the state of the checkboxes will be reflected appropriately in your FaxForm. faxStatusList object.
上面的代码段将显示以 br 标记分隔的复选框项目列表。对复选框状态所做的任何更改都将适当反映在您的传真表格中。faxStatusList 对象。

