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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:05:16  来源:igfitidea点击:

Displaying objects from a Set collection in Datatable JSF does not work

javajsfjakarta-ee

提问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 Setin 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 Listdoes that and this data structure is the most sensible data structure to represent the value of an UIDatacomponent. The DataModelinterface, which represents the wrapped value of the UIDatacomponents 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 Listinterface 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 Setfor n-m relationships), the JSF spec team has for the upcoming JSF 2.2 finally decided to let the DataModelinterface support the Collectioninterface instead of alone the List, with help of the new CollectionDataModelimplementation. This supports sets as well. See also JSF spec issue 479. You should only keep in mind to use LinkedHashSetinstead of HashSet, certainly if your intention is to have an editable data table. A LinkedHashSetmaintains the ordering of the elements.

经过漫长的决策过程(特别是由通常Set用于 nm 关系的 Hibernate/JPA 社区推动),JSF 规范团队对于即将到来的 JSF 2.2 最终决定让DataModel接口支持Collection接口而不是单独的List,在新的帮助下CollectionDataModel执行。这也支持集合。另请参阅JSF 规范问题 479。您应该只记住使用LinkedHashSet而不是HashSet,当然如果您打算拥有可编辑的数据表。ALinkedHashSet维护元素的顺序。