Java 使用jsp迭代对象列表';显示标签
我是jsp新手,对其导出功能的显示标记感兴趣。比如说,我有一个简单的结构:Java 使用jsp迭代对象列表';显示标签,java,jsp,displaytag,Java,Jsp,Displaytag,我是jsp新手,对其导出功能的显示标记感兴趣。比如说,我有一个简单的结构: public class MyActionBean implements ActionBean{ List<Country> countries; // getters and setters and some other un-related logic } public class Country { List<String> countryName;
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
}
公共类MyActionBean实现ActionBean{
列出国家名单;
//getter和setter以及其他一些不相关的逻辑
}
公营国家{
列出国家名称;
列出账目;
//getter和setter以及其他一些不相关的逻辑
}
公共类帐户{
私营金融实体;
//getter和setter以及其他一些不相关的逻辑
}
公共财政{
字符串entityName;
//getter和setter以及其他一些不相关的逻辑
}
现在,我想制作一个表,它将有两列-国家名称和entityName(FinancialEntity)
以某种方式获取与国家相关的所有实体名称?
所以,本质上,我想对账户进行迭代,得到所有的金融实体。我不知道如何在带有displaytag的JSP中实现这一点。我尝试使用c:forEach和display:setProperty标记,但看起来这个标记并不是为了这些目的。我被卡住了:(
提前感谢:)您不必在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();
}
}
}
公共类国家金融属性{
私人国家;
公共国家财政(国家){
这个国家=国家;
}
公共字符串getCountryName(){
返回此.country.getName();
}
公共列表getFinancialEntityNames(){
List financialEntityNames=新阵列列表
for(帐户:this.country.getAccounts(){
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>
- ${account.financialEntity.name}
顺便说一句,这很可能是CountryFinancialEntity的外观,但如果您要有其他列,则使用类似CountryFinancialEntity的对象,但将其称为TableRowModel。您不必在jsp中执行此工作。您可以在model对象和controlle中执行此工作r
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();
}
}
}
公共类国家金融属性{
私人国家;
公共国家财政(国家){
这个国家=国家;
}
公共字符串getCountryName(){
返回此.country.getName();
}
公共列表getFinancialEntityNames(){
List financialEntityNames=新阵列列表
for(帐户:this.country.getAccounts(){
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>
- ${account.financialEntity.name}
顺便说一句,这很可能是CountryFinancialEntity的外观,但如果您打算使用其他列,则使用类似CountryFinancialEntity的对象,但将其称为TableRowModel。您好,非常感谢您的回复。但是,这不起作用。原因是,会有将有多个列(我只提到了两个,但还有更多),我需要通过jsp进行操作:(您好,非常感谢您的回复。但是,这不起作用。原因是,将有多个列(我只提到了两个,但还有更多),我需要通过jsp进行操作:(