JSF Datatable Map Iteration问题?

时间:2013-10-10 19:06:54

标签: jsf

我在My JSF页面中有这段代码

<h:dataTable value="#{announcementBean.monthList}" var="myMapEntry">
    <h:column>
        <h:outputText value="#{myMapEntry.key}" />
        <h:dataTable value="#{myMapEntry.value}" var="family">
            <h:column>
                <h:outputText value="#{family.monthName}" />
            </h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

我的数据是这样的

{2007=[com.kuber.vo.AnnouncementVO@87281f], 2010=[com.kuber.vo.AnnouncementVO@15b4804, com.kuber.vo.AnnouncementVO@1d510be], 2012=[com.kuber.vo.AnnouncementVO@13e49a8]}

但生成的HTML页面什么都没显示。这段代码可能会出现什么问题?

private TreeMap<String, List<AnnouncementVO>> monthList = new TreeMap<String, List<AnnouncementVO>>();

public void fetchMonthList(List<AnnouncementVO> dataList) {
    for (AnnouncementVO announcementVO : dataList) {
        String year = getYearFromDate(announcementVO.getAnnouncementDt());
        if (!monthList.containsKey(year)) {
            ArrayList listRecord = new ArrayList();
            announcementVO.setMonthName(getMonthANDYearFromDate(announcementVO.getAnnouncementDt()));
            listRecord.add(announcementVO);
            monthList.put(year, listRecord);
        } else {
            ArrayList list = (ArrayList) monthList.get(year);
            announcementVO.setMonthName(getMonthANDYearFromDate(announcementVO.getAnnouncementDt()));
            list.add(announcementVO);
            monthList.put(year, list);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

public ArrayList<Entry<String, List<AnnouncementVO>>> getYearList() {
        Set<Entry<String, List<AnnouncementVO>>> yearSet = monthList.entrySet();
        return new ArrayList<Map.Entry<String, List<AnnouncementVO>>>(yearSet);
    }

然后将jsf代码monthList更改为yearList

<h:dataTable value="#{announcementBean.yearList}" var="myMapEntry">
    <h:column>
        <h:outputText value="#{myMapEntry.key}" />
        <h:dataTable value="#{myMapEntry.value}" var="family">
            <h:column>
                <h:outputText value="#{family.monthName}" />
            </h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

答案 1 :(得分:0)

您无法使用JSF Datatable迭代Map接口。您有两种选择:

1)使用JSTL c:forEach:

迭代TreeMap
<c:forEach items="#{announcementBean.monthList}" var="myMapEntry">
        <h:outputText value="#{myMapEntry.key}" />
        <h:dataTable value="#{myMapEntry.value}" var="family">
            <h:column>
                <h:outputText value="#{family.monthName}" />
            </h:column>
        </h:dataTable>
</c:forEach>

2)使用Entry界面:

public List<Entry<String, List<AnnouncementVO>>> getObject() {
   List<Entry<String, List<AnnouncementVO>>> obj = 
              new ArrayList<Entry<String, List<AnnouncementVO>>>();
   obj.addAll(monthList.entrySet());
}