Jsf 2 从集合中检索值并显示在数据表中
我想使用datatable在view_lang.xhtml中显示语言,下面是我的类 CountryBean.javaJsf 2 从集合中检索值并显示在数据表中,jsf-2,Jsf 2,我想使用datatable在view_lang.xhtml中显示语言,下面是我的类 CountryBean.java private ArrayList<Country> existingCountryList; public ArrayList<Country> getExistingCountryList() { System.out.println("CountryBean.getExistingCountryList::Enter");
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;
}
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打印。
我必须使用forEach的syntex是什么,如果是,那么如何使用。
thnx您可以为此使用
,但它不支持设置(因为它不按索引排序)。您需要将其转换为列表
或数组。如果您使用的是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}{loop.last?'':'','}
注意:如果languageName
实际上是一个集合
而不是列表
,显然要在那里使用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>