来自数据库的displaytag动态列

时间:2014-04-21 12:17:19

标签: spring struts2 displaytag

我想在struts2 web应用程序中使用display标签来显示数据库中的数据。数据显示动态更改表名并显示数据。如何在jsp中显示动态列。 我使用以下代码。它只工作一排。如何在显示表中添加多行?

  public class TableDataDyno {

private ArrayList<Object> dynaBeans;
public TableDataDyno()
{
    dynaBeans = new ArrayList<Object>();

    dynaBeans.add(new DynaBean("One","5"));
    dynaBeans.add(new DynaBean("One","6"));
    dynaBeans.add(new DynaBean("One","7"));
    dynaBeans.add(new DynaBean("One","8"));
    dynaBeans.add(new DynaBean("One","9"));

    dynaBeans.add (new DynaBean("Two", "5"));
    dynaBeans.add (new DynaBean("Two", "6"));
    dynaBeans.add (new DynaBean("Two", "7"));
    dynaBeans.add (new DynaBean("Two", "8"));
    dynaBeans.add (new DynaBean("Two", "9"));


    dynaBeans.add(new DynaBean("Three", "5"));
    dynaBeans.add(new DynaBean("Three", "6"));
    dynaBeans.add(new DynaBean("Three", "7"));
    dynaBeans.add(new DynaBean("Three", "8"));
    dynaBeans.add(new DynaBean("Three", "9"));

}
public ArrayList<Object> getdynaBeans() {
    return dynaBeans;
}

    public class DynaBean {
private String title;
  private String value;
      public DynaBean(String title, String value) {
        this.title = title;
        this.value = value;
    }
        public String getTitle() {
        return title;
    }
    public String getValue() {
        return value;
    }

}

         java.util.ArrayList outerBeans = new java.util.ArrayList();
               for(int i=0; i< 2 ; i++)
               {
                 outerBeans.add(new TableDataDyno());
               }
                System.out.println(outerBeans);
   request.setAttribute("items", outerBeans);



        <display:table uid="item" name="items">
   <c:forEach var="cl" items="${item.dynaBeans }" varStatus="index">
       <display:column title="${cl.title}" property="dynaBeans[${index.index}].value"  />
   </c:forEach>

输出为:enter image description here

预期产出: enter image description here

1 个答案:

答案 0 :(得分:1)

稍微改变一下

TableDataDyno.java

public class TableDataDyno {
private ArrayList<DynaBean> dynaBeans;

public TableDataDyno(int number)
{
        dynaBeans = new ArrayList<DynaBean>();
        dynaBeans.add(new DynaBean("One",number));
        dynaBeans.add (new DynaBean("Two", number));
        dynaBeans.add(new DynaBean("Three", number));
}
public ArrayList<DynaBean> getdynaBeans() {
    return dynaBeans;
}
}
jsp文件

中的

java.util.ArrayList<TableDataDyno> outerBeans = new java.util.ArrayList<TableDataDyno>();
for(int i=5; i< 10 ; i++)
{
    outerBeans.add(new TableDataDyno(i));
}

request.setAttribute("items", outerBeans);

<display:table uid="item" name="items">
  <c:forEach var="cl" items="${item.dynaBeans }" varStatus="index">
      <display:column title="${cl.title}" property="dynaBeans[${index.index}].value"  />
  </c:forEach>
</display:table>

提供输出

  

一二三

     

5 5 5

     

6 6 6

     

7 7 7

     

8 8 8

     

9 9 9

但仍然针对不同的值我们需要解决方案

如果我将 DynaBean.java 设为

 public class DynaBean {
private String title;
  private List<String> value;
      public DynaBean(String title, List<String> value) {
        this.title = title;
        this.value = value;
    }
        public String getTitle() {
        return title;
    }
    public List<String> getValue() {
        return value;
    }
 }

TableDataDyno.java

 public class TableDataDyno {
private ArrayList<DynaBean> dynaBeans;

  public TableDataDyno()
{
        dynaBeans = new ArrayList<DynaBean>();
        List<String> arr1 = new ArrayList<String>();
        arr1.add("5");
        arr1.add("6");
        arr1.add("7");
        arr1.add("10");

        dynaBeans.add(new DynaBean("One",arr1));
        List<String> arr2 = new ArrayList<String>();
        arr2.add("5");
        arr2.add("6");
        arr2.add("7");
        arr2.add("11");

        dynaBeans.add (new DynaBean("Two", arr2));

        List<String> arr3 = new ArrayList<String>();
        arr3.add("5");
        arr3.add("6");
        arr3.add("7");
        arr3.add("13");

        dynaBeans.add(new DynaBean("Three", arr3));

}
public ArrayList<DynaBean> getdynaBeans() {
    return dynaBeans;
}
 }

然后输出

One          Two     Three 
     

[5,6,7,10] [5,6,7,11] [5,6,7,13]

     

[5,6,7,10] [5,6,7,11] [5,6,7,13]

仍未获得预期的输出

现在 jsp 页面中有一个小技巧

 <display:table uid="item" name="items">
    <c:forEach var="cl" items="${item.dynaBeans }" varStatus="index">
        <display:column title="${cl.title}"    >
            <display:table uid="b" name="${cl.value}">
                    <c:out value="${b}"></c:out>
            </display:table>
        </display:column>
   </c:forEach>
  </display:table>   

输出预期输出

  

一二三

     

5 5 5

     

6 6 6

     

7 7 7

     

10 11 13

最后我们得到了!!!! :)