2013-10-16 36 views
1

我是jsp的新手,對顯示標籤的輸出功能很感興趣。我說,我有這樣的簡單結構:使用jsp的displaytag遍歷對象列表

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 
} 

現在,我想要做一個表,該表將有兩列 - 國家名稱和的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> 

所以,基本上我想迭代賬戶並獲得所有金融實體。我不知道如何在JSP中使用displaytag來做到這一點。我嘗試使用c:forEach並顯示:setProperty標記,但它看起來像這個標記不是爲了這些目的。我致命陷:(

預先感謝您:)

回答

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。

+0

你好,非常感謝你的回覆。但是,這不起作用。原因是,會有多個列(我只提到了兩個,但還有更多),我需要通過jsp :( – user1039063