JSP / STRUTS2:< display:table> :如何从嵌套列表创建表?

时间:2012-12-24 19:25:12

标签: jsp struts2

我很难在JSP页面上生成一个display:table,它有3层嵌套列表:

我的主要类是“ExpenditureTotalsByRegions”,其中包含“ExpenditureTotalsByTypes”列表 “ExpenditureTotalsByTypes”有一个列表“ExpenditureTotalsByProjects”

主要问题是:此表格中的行数和列数可能会有所不同: 区域数量会有所不同; 类型数量可能有所不同/地区; 项目数量/类型

JSP页面获取“ExpenditureTotalsByRegions”列表,应该如下所示:

Region 1
----------------------------------------------------------
Types |  Project 1 | Project 2
----------------------------------------------------------
type1 | $1,000     | $2,000
type2 | $2,000     | $2,500


Region 2
----------------------------------------------------------
Types |  Project 1 | Project 3  |Project 4
----------------------------------------------------------
type3 | $3,000     | $4,000     | $5,000
type4 | $3,500     | $4,500     | $5,500
type5 | $3,550     | $4,550     | $5,550

(在上面的示例中:'区域1',区域2'是区域的名称,'类型1','类型2'等是类型的名称和'项目1',' Project2'等是项目的名称,$支出是项目中的实际支出金额)

主要班级:

public class ExpenditureTotalsByRegions {

    protected String fRegion = null;
    protected List<ExpenditureTotalsByTypes> fTypeExpenditures = new ArrayList<ExpenditureTotalsByTypes>();

    public ExpenditureTotalsByRegions(String region,List<ExpenditureTotalsByTypes> typeExpenditures) {
        fRegion = region;
        fTypeExpenditures = typeExpenditures;
    }

    public String getRegion() {
        return fRegion;
    }

    public void setRegion(String region) {
        fRegion = region;
    }

    public List<ExpenditureTotalsByTypes> getTypeExpenditures() {
        return fTypeExpenditures ;
    }

    public void setTypeExpenditures( List<ExpenditureTotalsByTypes> typeExpenditures) {
    fTypeExpenditures = typeExpenditures;
    }

    }

现在,'ExpenditureType'类看起来像:

    public class ExpenditureTotalsByTypes{

    protected String fTypeName;
    protected List<ExpenditureTotalsByProjects> fProjectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();


    public String getTypeName() {
    return fTypeName;
    }

    public void setTypeName(String typeName) {
        fTypeName= typeName;
    }


    public List<ExpenditureTotalsByProjects> getProjectExpenditures() {
        return fProjectExpenditures;
    }

    public void setProjectExpenditures( List<ExpenditureTotalsByProjects> projectExpenditures) {
        fProjectExpenditures = projectExpenditures;
    }

}

现在,“ExpenditureTotalsByProjects:”类似于:

public class ExpenditureTotalsByProjects {

    protected String fProjectName;
    protected Double fProjectExpenditure;


    public ExpenditureTotalsByProjects( String projectName, Double projectExpenditure)   {
        fProjectName = projectName;
        fProjectExpenditure = projectExpenditure;        
    }


    public String getProjectName() {        
        return fProjectName;
    }      

    public void setProjectName( String projectName ) {        
        fProjectName = projectName;
    }

    public Double getProjectExpenditure() {        
        return fProjectExpenditure;
    }      

    public void setProjectExpenditure( Double projectExpenditure ) {        
        fProjectExpenditure = projectExpenditure;
    }


}

我的动作类看起来像:

public class ExpendituresTotalsReportByRegionsAction extends ActionBase {

    protected List<ExpenditureTotalsByRegions> fTableData = new ArrayList<ExpenditureTotalsByRegions>();

    public ExpendituresTotalsReportByRegionsAction(JpaTransactionManager transactionManager) {
        super(transactionManager);
    }

    @Override
    public String execute() throws Exception {
        if (fYear != 0) {
             fTableData = reportDao.getExpenditureTotalsReportByRegions(fYear);
         }

        return SUCCESS;
    }


    public List<ExpenditureTotalsByRegions> getTableData() {
        return fTableData;
    }

}

现在,这是我尝试在JSP页面上构建表并获得运行时错误的方式:

<body>
<div class="container">
<div class="prepend-1"><br>
    <display:table id="reportTable"
                   name="tableData"
                   requestURI=""
                   cellspacing="0px"
                   export="true"
                   class="displaytagTable">
        <display:setProperty name="decorator.media.html"
                             value="gov.doi.expenditures.decorator.TotalSpeciesDecorator"/>
        <display:setProperty name="decorator.media.rtf"
                             value="gov.doi.expenditures.decorator.ItextTotalSpeciesDecorator"/>
        <display:setProperty name="decorator.media.pdf"
                             value="gov.doi.expenditures.decorator.ItextTotalSpeciesDecorator"/>

            <display:caption class="titleText ">${reportTable.region}</display:caption>

            <c:forEach items="${reportTable.typeExpenditures}" var="typeList"> 

                <display:column title="Types" property="${typeList}.typeName"/>

                <c:forEach items="${typeList.projectExpenditures}" var="projectList">
                    <display:column title="${projectList}.projectName" property="${projectList}.projectExpenditure"/>
                </c:forEach>

            </c:forEach> 

    </display:table>
</div>
</div>

</body>

测试数据:

 List<ExpenditureTotalsByProjects> projectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();
         List<ExpenditureTotalsByTypes> TypesExpenditures = new ArrayList<ExpenditureTotalsByTypes>();
         List<ExpenditureTotalsByRegions> regionExpenditures = new ArrayList<ExpenditureTotalsByRegions>();


         //Test data for reg1 
         projectExpenditures.add(new ExpenditureTotalsByProjects("project1",1000.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project2",2000.00) );

         //1st Types with 2 projects in reg1
         ExpenditureTotalsByTypes sp1 = new ExpenditureTotalsByTypes();
         sp1.setTypes("Types1");
         sp1.setProjectExpenditures(projectExpenditures);
         TypesExpenditures.add(sp1);


         //2nd Types with 2 projects in reg1
         projectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();
         projectExpenditures.add(new ExpenditureTotalsByProjects("project1",1500.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project2",2500.00) );

         ExpenditureTotalsByTypes sp2 = new ExpenditureTotalsByTypes();
         sp2.setTypes("Types2");
         sp2.setProjectExpenditures(projectExpenditures);
         TypesExpenditures.add(sp2);


         //regionExpenditures.add(new ExpenditureTotalsByRegions("reg1",TypesExpenditures));

         tableData.add(new ExpenditureTotalsByRegions("reg1",TypesExpenditures));

         //Test data for reg2 
         TypesExpenditures = new ArrayList<ExpenditureTotalsByTypes>();

         projectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();
         projectExpenditures.add(new ExpenditureTotalsByProjects("project3",3000.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project4",4000.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project5",5000.00) );

         sp1 = new ExpenditureTotalsByTypes();
         sp1.setTypes("Types3");
         sp1.setProjectExpenditures(projectExpenditures);
         TypesExpenditures.add(sp1);

         projectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();
         projectExpenditures.add(new ExpenditureTotalsByProjects("project3",3500.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project4",4500.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project5",5500.00) );

         sp2 = new ExpenditureTotalsByTypes();
         sp2.setTypes("Types4");
         sp2.setProjectExpenditures(projectExpenditures);
         TypesExpenditures.add(sp2);

         projectExpenditures = new ArrayList<ExpenditureTotalsByProjects>();
         projectExpenditures.add(new ExpenditureTotalsByProjects("project3",3550.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project4",4550.00) );
         projectExpenditures.add(new ExpenditureTotalsByProjects("project5",5550.00) );

         ExpenditureTotalsByTypes sp3 = new ExpenditureTotalsByTypes();
         sp3.setTypes("Types5");
         sp3.setProjectExpenditures(projectExpenditures);
         TypesExpenditures.add(sp3);


         tableData.add(new ExpenditureTotalsByRegions("reg2",TypesExpenditures));

获取错误:“java.lang.RuntimeException:java.lang.RuntimeException:javax.servlet.jsp.JspException:查找属性时出错”但我还需要使用'forEach'来帮助构建表。请帮忙

如果您需要更多信息,请与我们联系。 提前致谢。

1 个答案:

答案 0 :(得分:1)

一种方法是:

<body>
<div class="container">
<div class="prepend-1"><br>
    <c:forEach items="${tableData}" var="regionList"> 
        <display:table id="TypeExpenditures"
                    name="${regionList.typeExpenditures}"
                    requestURI=""
                    cellspacing="0px"
                    export="true"
                    class="displaytagTable">


                <display:caption class="titleText ">${regionList.region}</display:caption>
                <display:column title="Types" property="typeName"/>

                <c:forEach items="${TypeExpenditures.projectExpenditures}" var="projectList"> 
                    <display:column title="${projectList.projectName}"  class='alignRight'>
                       $ ${projectList.projectExpenditure}
                    </display:column>
                </c:forEach>


        </display:table>
    </c:forEach>
</div>
</div>

</body>