在选定的一个菜单列表中显示哈希表



我有一个哈希表,我想在selecteOneMenu中显示它,知道怎么做吗?

itemCat = new Hashtable<Integer,String>();
    itemCat.put(0,"Functional");
    itemCat.put(1, "Interoperability");
    itemCat.put(2, "Load");
    itemCat.put(3, "Performance");
    itemCat.put(4, "Disponibility");
    itemCat.put(5, "Security");
    itemCat.put(6, "Usability");
    itemCat.put(7, "Other");
 <p:selectOneMenu value="#{projectRequirementManagementMB.selectedCat}" filter="true" filterMatchMode="startsWith" >  
                                       <f:selectItem itemLabel="Select One" itemValue="" />  
                                <f:selectItems value=" {projectRequirementManagementMB.hereShouldbeCategoryValues}" />  
                                     </p:selectOneMenu>  

任何想法都会被理解

<f:selectItems>已经支持 Map 接口:

<f:selectItems value="#{projectRequirementManagementMB.itemCat}" />

用只是

public Map<Integer, String> getItemCat() {
   return itemCat;
}

但是,映射键被解释为项目标签,而映射值被解释为项目值。如果无法交换模型中的映射键/值,则需要在视图中按如下方式交换它们<f:selectItems>,前提是您的环境支持 EL 2.2(您的问题历史记录证实了这一点)。

<f:selectItems value="#{projectRequirementManagementMB.itemCat.entrySet()}" 
     var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />

另请参阅:

  • 我们的h:selectOneMenu维基页面

具体问题无关,Hashtable是一个相当遗留的数据结构(来自Java 1.0,1996!!),你应该改用它的后继HashMap。然而,这反过来又Hashtable另一个问题:物品本质上根本没有订购。如果您想保持广告顺序,则可以使用LinkedHashMap,或者如果您希望对键进行自动排序,则可以使用TreeMap

itemCat = new LinkedHashMap<Integer, String>();

最新更新