“#{serviceCatalogueController.allMap [category.key]}”:在类型java.util.HashMap $ KeySet上找不到属性'key'

时间:2015-02-03 17:10:19

标签: jsf primefaces hashmap el datalist

我正在尝试使用inputText和inputTextArea中的值填充地图。还有日期。在backbean中,我有一张类似Map>

的地图

基本上,类别可以有许多属性。这些属性是输入字段。对于多选,下拉和单选,我需要的是设置属性。但对于TextArea,textField和Date我需要设置属性加上值,这就是我拥有String的原因。

我的jsf看起来像

              <p:dataList id="serviceCatalogueCriteria" var="category" value="#{serviceCatalogueController.allMap.keySet()}">
                    <div id="categoryDiv" style="overflow: hidden;">
                        <div class="category-header">
                            <h:outputText value="#{serviceCatalogueController.allMap[category.key]}" />
                        </div>
                        <div>
                            <!-- Text field, text area or date -->
                            <div class="category-text">
                                <ui:fragment
                                    rendered="#{categoryService.isTextField(category) or categoryService.isTextArea(category) or categoryService.isDate(category)}">
                                    <ui:fragment
                                        rendered="#{categoryService.isTextField(category)}">
                                        <p:dataList var="attribute" value="#{category.entrySet()}" >
                                            <p:inputText 
                                                value="#{category[attribute.key]}" />
                                        </p:dataList>
                                    </ui:fragment>
                                    <ui:fragment
                                        rendered="#{categoryService.isTextArea(category)}">
                                        <p:dataList var="attribute" value="#{category.entrySet()}" >
                                            <p:inputTextarea rows="3" cols="70"
                                                value="#{category[attribute.key]}"/>
                                        </p:dataList>
                                    </ui:fragment>
                                    <ui:fragment rendered="#{categoryService.isDate(category)}">
                                        <p:dataList var="attribute" value="#{category.entrySet()}" >
                                            <p:calendar
                                                value="#{category[attribute.key]}"
                                                showOn="button">
                                            </p:calendar>
                                        </p:dataList>
                                    </ui:fragment>
                                </ui:fragment>
                            </div>
                    <p:separator />
                </p:dataList>

我的背豆看起来像

private Map<Category,Map<Attribute,String>> allMap;
//setter and getter

我得到的错误是

javax.el.PropertyNotFoundException: /resources/PWAssessment/ServiceCatalogueFilters.xhtml @24,84 value="#{serviceCatalogueController.allMap[category.key]}": Property 'key' not found on type java.util.HashMap$KeySet

为什么这不起作用?

1 个答案:

答案 0 :(得分:2)

有2个错误。

  1. <p:dataList>无法迭代Set<E>(也不Map<K, V>)。它只能迭代List<E>E[]DataModel<E>

  2. 即使它有效,var="category"代表Category实例本身,而不是Map。如果<p:dataList>支持Set<E>,则应该使用

    <h:outputText value="#{serviceCatalogueController.allMap[category]}" />
    

    并且,等效地,在代码中,#{category.entrySet()}也不会有效,因为#{category}将代表Category实例本身。

  3. <p:dataList>支持迭代数组。所以,这应该做:

    <p:dataList value="#{serviceCatalogueController.allMap.entrySet().toArray()}" var="categoryEntry">
        This is Category: #{categoryEntry.key}
        This is Map&lt;Attribute,String&gt;: #{categoryEntry.value}
        ...
        <p:dataList value="#{categoryEntry.value.entrySet().toArray()}" var="attributeEntry">
            This is Attribute: #{attributeEntry.key}
            This is String: #{attributeEntry.value}
    

    但这是非常低效的。我强烈建议您重新构建模型,使其只有List<CategoryDTO>之类的内容。否则,请抓取<c:forEach>,它支持有效地迭代Map

    <c:forEach items="#{serviceCatalogueController.allMap}" var="categoryEntry">
        This is Category: #{categoryEntry.key}
        This is Map&lt;Attribute,String&gt;: #{categoryEntry.value}
        ...
        <c:forEach items="#{categoryEntry.value}" var="attributeEntry">
            This is Attribute: #{attributeEntry.key}
            This is String: #{attributeEntry.value}