i want using display tag in struts2 web application for display data from database. The data display dynamically change table name and show the data. how to display dynamic column in jsp. i used following code. it's working one row only. how to add multiple row in display table?
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>
output is:
Expected Output:
Made little bit change
In 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;
}
}
in jsp file
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>
gives output
One Two Three
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
But still for different values we need solution
If I make DynaBean.java as
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;
}
}
And TableDataDyno.java as
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;
}
}
then output is
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]
Still not getting expected output
Now there is a little trick in jsp page
<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>
And output is your expected output
One Two Three
5 5 5
6 6 6
7 7 7
10 11 13
Finally we got it !!!! :)