user1039063
user1039063

Reputation: 211

Iterating over list of objects with jsp's displaytag

I am new to the jsp and interested in display tag for its export functionality. Say, I have this simple structure:

 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
}

Now, I want to make a table, which will have two columns - country name and 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>

So, essentially I want to iterate over the accounts and get all the financial entities. I do not know how to do that in JSP with displaytag. I tried to use c:forEach and display:setProperty tag, but it look like this tag is not for these purposes. I am deadly stuck :(

Thank you in advance :)

Upvotes: 1

Views: 3867

Answers (1)

hooknc
hooknc

Reputation: 5001

You don't have to do the work in the jsp. You could do that work in a model object and the controller.

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

Then make a list of these objects for all your countries and pass this object to your view (jsp).

Hopefully this will simplify the use of the display tags and allow you to use a c:forEach tag.

EDIT

If you MUST do this work in the jsp.

I would recommend just passing the list of countries. The MyActionBean really doesn't help and could cause confusion.

Your jsp will look something like the following:

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

BTW, this is most likely how the CountryFinancialEntity would look as well come to think of it, but if you're going to have other columns then using something like the CountryFinancialEntity object, but calling it a TableRowModel instead.

Upvotes: 1

Related Questions