java 在 Datatable JSF 中显示 Set 集合中的对象不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7711703/
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
Displaying objects from a Set collection in Datatable JSF does not work
提问by LuckyLuke
Any reason why a such asSet<MyObject> objects = new HashSet<MyObject>();
shouldn't work in the JSF Datatable? It works with List.
有什么理由Set<MyObject> objects = new HashSet<MyObject>();
不能在 JSF 数据表中工作?它适用于列表。
回答by BalusC
As to why a Set
in general isn't supported, this is because this data structure is never intented to hold a collection of objects which is ordered by an index. The List
does that and this data structure is the most sensible data structure to represent the value of an UIData
component. The DataModel
interface, which represents the wrapped value of the UIData
components and holds the row indexes and remembers the current row for iteration on render and form submit processing on postback, supports from the Java collection classes only the List
interface in flavor of ListDataModel
.
至于为什么Set
一般不支持 a ,这是因为此数据结构从未打算保存按 index 排序的对象集合。这样List
做和这个数据结构是最合理的数据结构来表示一个UIData
组件的价值。该DataModel
接口表示UIData
组件的包装值并保存行索引并记住当前行以进行渲染迭代和回发时表单提交处理,仅支持 Java 集合类List
中的ListDataModel
.
After a long decision process (especially pushed by Hibernate/JPA community who generally uses Set
for n-m relationships), the JSF spec team has for the upcoming JSF 2.2 finally decided to let the DataModel
interface support the Collection
interface instead of alone the List
, with help of the new CollectionDataModel
implementation. This supports sets as well. See also JSF spec issue 479. You should only keep in mind to use LinkedHashSet
instead of HashSet
, certainly if your intention is to have an editable data table. A LinkedHashSet
maintains the ordering of the elements.
经过漫长的决策过程(特别是由通常Set
用于 nm 关系的 Hibernate/JPA 社区推动),JSF 规范团队对于即将到来的 JSF 2.2 最终决定让DataModel
接口支持Collection
接口而不是单独的List
,在新的帮助下CollectionDataModel
执行。这也支持集合。另请参阅JSF 规范问题 479。您应该只记住使用LinkedHashSet
而不是HashSet
,当然如果您打算拥有可编辑的数据表。ALinkedHashSet
维护元素的顺序。