【问题标题】:retrive value from collection and show in datatable从集合中检索值并显示在数据表中
【发布时间】:2013-09-04 09:16:03
【问题描述】:

我想使用数据表在 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;


}

国家.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>

我可以使用语言获取国家/地区对象,但无法在数据表中打印。 我必须使用 forEach 什么是 syntex,如果是,那么如何。 谢谢

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

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

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

    更新,根据 cmets,您不想以逗号分隔打印,您可以这样做:

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

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

    【讨论】:

    • 它的工作,我使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多