java 在 JSF+Facelets 中迭代 HashMap.values()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/310410/
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-29 11:53:57  来源:igfitidea点击:

Iterate over HashMap.values() in JSF+Facelets

javajsfdatatable

提问by Matt McMinn

I'm using JSF/Facelets, and I'm trying to iterate over some Document objects (custom object) that I'm keeping in a HashMap. When the page is loaded, I'm getting the error "Property 'name' not found on type java.util.HashMap$Values". Here's what's in my backing bean:

我正在使用 JSF/Facelets,我正在尝试迭代一些我保存在 HashMap 中的 Document 对象(自定义对象)。加载页面时,我收到错误“在类型 java.util.HashMap$Values 上找不到属性 'name'”。这是我的支持 bean 中的内容:

private Map<String, Document> documents = new HashMap<String, Document>();

public Collection<Document> getDocuments(){
    return documents.values();
}

And in my xhtml page:

在我的 xhtml 页面中:

<h:dataTable id="documentTable"
    value="#{DocumentManager.allDocuments}"
    var="doc" rowClasses="list-row-odd, list-row-even"
    headerClass="table-header" styleClass="bordered">

    <h:column id="col_name">
         <f:facet name="header">Name</f:facet>
         ${doc.name}
    </h:column>
</h:dataTable>

If I change the getDocuments function to the following, it works (meaning the table is displayed without error), but I'm not sure why I need to put the values in the list for the JSF/Facelets page to display properly.

如果我将 getDocuments 函数更改为以下内容,则它可以工作(意味着该表显示没有错误),但我不确定为什么需要将这些值放入列表中以便 JSF/Facelets 页面正确显示。

public List<Document> getDocuments(){
    List<Document> rtrn = new ArrayList<Document>();
    for(Document doc : documents.values())
        rtrn.add(doc);
    return rtrn;
}

Shouldn't I be able to iterate over the Collection?

我不应该能够迭代集合吗?

回答by billjamesdev

Well, as it turns out, you can't just use any kind of collection type with dataTable, and for good reason. From the MyFaces 1.2 Spec, the value attribute must be:

好吧,事实证明,您不能只对 dataTable 使用任何类型的集合类型,这是有充分理由的。从MyFaces 1.2 Spec 开始, value 属性必须是:

An EL expression that specifies the data model that backs this table.

The value referenced by the EL expression can be of any type.

  • A value of type DataModel is used directly.
  • Array-like parameters of type Object[], java.util.List, java.sql.ResultSetor javax.servlet.jsp.jstl.sql.Resultare wrapped in a corresponding DataModelthat knows how to iterate over the elements.
  • Other values are wrapped in a DataModel as a single row.

Note in particular that unordered collections, eg Setare not supported. Therefore if the value expression references such an object then the table will be considered to contain just one element - the collection itself.

指定支持此表的数据模型的 EL 表达式。

EL 表达式引用的值可以是任何类型。

  • 直接使用 DataModel 类型的值。
  • 阵列状类型的参数Object[]java.util.Listjava.sql.ResultSetjavax.servlet.jsp.jstl.sql.Result包装在相应DataModelthat知道如何在迭代的元素。
  • 其他值作为单行包装在 DataModel 中。

特别注意Set不支持无序集合,例如。因此,如果值表达式引用了这样一个对象,那么该表将被视为仅包含一个元素 - 集合本身。

The Collection returned from HashSet.values() is not ordered and, therefore is not supported. If it was, you'd have no idea what order the rows in your table would be output, and refreshes of the page could re-order them randomly. Not good.

从 HashSet.values() 返回的 Collection 没有排序,因此不受支持。如果是这样,您将不知道表格中的行将被输出的顺序,并且页面的刷新可能会随机重新排序它们。不好。

The error you're getting is, from the last paragraph, it says that the datatable is treating your Collection as the row object, and Collection doesn't have a "name" property.

您收到的错误是,从最后一段开始,它表示数据表将您的 Collection 视为行对象,而 Collection 没有“名称”属性。