使用jsp的displaytag迭代对象列表

时间:2013-10-16 02:05:04

标签: java jsp displaytag

我是jsp的新手,对其导出功能感兴趣的display标签。说,我有这个简单的结构:

 public class MyActionBean implements ActionBean{
      List<Country> countries;
      // getters and setters and some other un-related logic
 }


public class Country {
    List<String> countryName;
    List<Accounts> accounts;
    // getters and setters and some other un-related logic
}


public class Accounts {
    private FinancialEntity entity;
    // getters and setters and some other un-related logic
}

public class FinancialEntity {
    String entityName;
    // getters and setters and some other un-related logic
}

现在,我想创建一个表,它将有两列 - country name和entityName(FinancialEntity)

     <display:table id="row" name="${myActionBean.countries}" class="dataTable" pagesize="30" sort="list" defaultsort="8"  export="true" requestURI="">
        <display:column title="Country" sortable="true" group="1" property="countryName" />
        <display:column title="Financial Entity"> somehow get all of the entity names associated with the country? </display:column>
     </display:table>

所以,基本上我想迭代账户并获得所有金融实体。我不知道如何在带有displaytag的JSP中这样做。我尝试使用c:forEach并显示:setProperty标签,但看起来这个标签不是出于这些目的。我被致命地卡住了:(

提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

您不必在jsp中完成工作。你可以在模型对象和控制器中完成这项工作。

public class CountryFinancialEntity {
    private Country country;
    public CountryFinancialEntity(Country country) {
        this.country = country;
    }
    public String getCountryName() {
        return this.country.getName();
    }
    public List<String> getFinancialEntityNames() {
        List<String> financialEntityNames = new ArrayList<String>
        for (Account account : this.country.getAccounts() {
            financialEntityNames.add(account.getFinancialEntity().getName();
        }
    }
}

然后为所有国家/地区列出这些对象,并将此对象传递给您的视图(jsp)。

希望这会简化显示标签的使用,并允许您使用c:forEach标签。

修改

如果你必须在jsp中完成这项工作。

我建议只传递国家/地区列表。 MyActionBean确实没有用,可能引起混淆。

您的jsp将如下所示:

<display:table id="country" name="countries">
    <display:column title="Country Name" property="name" />
    <display:column title="Financial Name" >
        <ul>
        <c:forEach var="account" items="${country.accounts}">
            <li>${account.financialEntity.name}</>
        <c:forEach>
        </ul>
    </display:column>
</display:table>
顺便说一下,这很可能是CountryFinancialEntity看起来如何看待它,但是如果你要使用其他列然后使用类似CountryFinancialEntity对象的东西,而是将其称为TableRowModel。