【问题标题】:binding to a Set in Spring在 Spring 中绑定到 Set
【发布时间】:2009-07-27 06:26:48
【问题描述】:
如果我有一个列表对象,我知道我可以使用下面的代码将类属性字段绑定到表单。
<c:forEach items="${items}" var="i" varStatus="itemsRow">
<input name="items[${itemsRow.index}].fieldName" type="text"/>
</c:forEach> <form:errors path="items" />
如果属性是 Set 对象,我该怎么办。我已经在 Controller 类中阅读了有关 initBinder 的信息,但它不起作用。任何人都可以在此处粘贴代码来完成此操作吗?
【问题讨论】:
标签:
java
spring
jsp
binding
set
【解决方案1】:
你需要使用复选框控件:
<form:form>
<table>
<tr>
<td>Interests:</td>
<td>
<%-- Approach 2: Property is of an array or of type java.util.Collection --%>
Quidditch: <form:checkbox path="preferences.interests" value="Quidditch"/>
Herbology: <form:checkbox path="preferences.interests" value="Herbology"/>
Defence Against the Dark Arts: <form:checkbox path="preferences.interests"
value="Defence Against the Dark Arts"/>
</td>
</tr>
</table>
</form:form>
在documentation查看更多信息
【讨论】:
-
Set 对象是 Set 并且需要显示文本字段以便可以修改图像名称等...上述方法适用于不输入文本字段的复选框,有人可以粘贴一些代码关于如何使用initbinder。我认为这个问题可以使用Controller类中的initBinder方法来解决
-