1

I have this code in My JSF Page

<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>

And my data is something like this

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

But the generated HTML page displays nothing. What can be issue in this code?

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);
        }
    }
}
4

2 回答 2

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>
于 2013-10-10T20:19:09.970 回答
0

您不能使用 JSF 数据表遍历 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)使用入口界面:

public List<Entry<String, List<AnnouncementVO>>> getObject() {
   List<Entry<String, List<AnnouncementVO>>> obj = 
              new ArrayList<Entry<String, List<AnnouncementVO>>>();
   obj.addAll(monthList.entrySet());
}
于 2013-10-10T20:34:21.877 回答