Spring表单绑定到c:forEach里面 [英] Spring form binding to Set inside c:forEach
问题描述
我在我的JSP中使用Spring 表单
表单绑定。我的命令对象包含一个 Set
,我想使用JSTL < c:forEach>
和为我的集合中的每个对象创建一个Spring < form:input>
。我已经看过如何使用列表
:
I am using the Spring form
tag library for form binding in my JSP. My command object contains a Set
and I would like to iterate over that set using JSTL <c:forEach>
and create a Spring <form:input>
for each object in my set. I have seen how to do this with List
:
<form:form>
<c:forEach items="${itemList}" var="item" varStatus="status" >
<form:input path="itemList[${status.index}].name" />
</c:forEach>
</form:form>
我的问题是我使用无序的 Set
我不相信我可以使用索引
属性。将 Set
的内容添加到Spring绑定表单中的最佳做法是什么?我想我只需要转换为列表
,但我对任何可能的替代方案感兴趣。
My problem is that I am using an unordered Set
and I don't believe that I can use the index
property. What is the best practice for adding the contents of a Set
to a Spring-bound form? I think I just need to convert to a List
, but I am interested in any possible alternatives.
推荐答案
您的表单支持对象应该包含列表
中的这些数据。迭代次序不能保证意味着任何事情 - 当然没有一套索引
的概念,因此迭代集合没有什么意义。我想象你可能有一定的期望顺序,这些元素出现在页面上。
Your form-backing object should contain this data in a List
, really. It doesn't make much sense to iterate over the set as the iteration order is not guaranteed to mean anything - and of course there is no concept of an index
for a Set. I would imagine you probably have a certain desired order for these elements to appear on the page.
这篇关于Spring表单绑定到c:forEach里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!