6

我的页面中有一个 selectonemenu,我从我的支持 bean 中获取它的内容。但是selectonemenu的内容没有排序。这是代码......

    <p:selectOneMenu id="Department" value="#{search.selectedDepartment}">  
            <f:selectItem itemLabel="Select Department" itemValue="" />  
            <f:selectItems value="#{search.departments}" />  

        </p:selectOneMenu> 

还有我的豆...

public class SearchBean implements Serializable {

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

private Map<String, String> departments = new HashMap<String, String>();
private String selectedDepartment;


    departments.put("department1",
            "department1");
    departments.put("department2",
            "department2");
    departments.put("department3", "department3");
departments.put("department4", "department4");
departments.put("department5", "department5");





//getters setters...


}

并且selectonemenu的内容分别不是department1,department2,department3,department4,department5,没有排序。

4

2 回答 2

16

它没有排序,因为HashMap没有可预测的迭代顺序。试试LinkedHashMap吧。

于 2013-05-12T10:23:19.727 回答
6

使用LinkedHashMap它是按顺序排列的,即你按照放置项目的顺序得到它,但是如果你使用TreeMap,那么它是按字母顺序排序的

于 2016-12-01T08:54:26.807 回答