从集合中检索值并在datatable中显示

时间:2013-09-04 09:16:04

标签: jsf-2

我想使用datatable在view_lang.xhtml中显示语言,下面是我的类

CountryBean.java

private ArrayList<Country> existingCountryList;
public ArrayList<Country> getExistingCountryList() {


    System.out.println("CountryBean.getExistingCountryList::Enter");


        existingCountryList = new ArrayList<Country>();
        existingCountryList.addAll(getCountryService().getExistingCountry());
        System.out.println("existingCountryList in countryBean"+existingCountryList);
        System.out.println("CountryBean.getExistingCountryList:::Exit");

return existingCountryList;


}

country.java

private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);

CountryLanguage.java

private CountryLanguageID countryLangPK = new CountryLanguageID();

CountryLanguageID.java

private Country country;
private Language language;

view_lang.xhtml

<h:dataTable id="existingCountry" var="countryLang" value="#{countryBean.existingCountryList}"
        style="width: 100%"  cellpadding="0"  cellspacing="1" border="0" class="role_detail_section" rowClasses="activity_white, activity_blue">

    <h:column>
            <f:facet name="header">
                <h:outputText value="Language(Code)" styleClass="heading_pm_det_white"/>
            </f:facet>

              <h:outputText value="#{countryLang.languageName}(#{countryLang.languageCode})" styleClass="heading_pm_det_white" />
        </h:column>

    </h:dataTable>

我能够使用语言获取国家/地区对象,但无法在datatabel中打印。 什么是syntex我必须使用forEach,如果是,那么如何。 日Thnx

1 个答案:

答案 0 :(得分:1)

您可以使用<ui:repeat>,但这不支持Set(因为它没有按索引排序)。您需要将其转换为List或数组。如果你使用的是EL 2.2,那么你可以直接在EL中使用Set#toArray()调用:

<ui:repeat value="#{countryLang.countryLanguage.toArray()}" var="countryLanguage">
    ...
</ui:repeat>

更新,根据评论,您希望以逗号分隔打印,以下是您可以执行的操作:

<ui:repeat value="#{countryLanguage.language.languageName}" var="languageName" varStatus="loop">
    #{languageName}#{loop.last ? '' : ', '}
</ui:repeat>

注意:如果languageName实际上是Set而不是List,那么显然在那里使用toArray()