You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Je...@nlgroup.ca on 2002/10/16 17:46:21 UTC

[Nested Tags] Please help to confirm a problem..

I am running on Struts 1.1b2 and Tomcat 4.04

I have provided a self contained JSP page that defines two objects and creates a list of lists and iterates though them three ways.
first by standard java, then logic:iterate tags and finally with nested tags.
My preference would be to use the nested tag version.
As near as I can tell, what I am doing is the same as many of the examples on Arrons site http://www.keyboardmonkey.com/
The only difference is a property that accesses an internal Map.  It seems to work well until the nested tags are used.

It my test the nested tags read all correct properties for the root object but when the child objects are accessed,the properties
are called on the root and not the current object as I would expect.

My Request:
Could someone please drop the JSP code into a test page and run it.
Observe the System.out from the wrapper object as well.
I would like to find out if there is a bug or that I am just a nut.

Thanks alot.
I need to decide on a direction for the remainder of our project, and would love to use the nested as much as possible.

PS:
A common requirement in our project to work with lists of lists.
many times each list has header information such as subtotal ect.

I am trying to use a generic wrapper that contains one internal List and a single getHeaderItem(headerName) property that will provide the specific.
My goal is to have my business delegate methods return this generic wrapper (or wrapper of wrappers) and keep the business structure out of the
actions ect


Jsp test page:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ page import="java.util.*" %>

<%
    //create sample data
    ArViewListImpl childWrapper = null;
    List childWrapperList = new ArrayList();

    List sampleGroupList = new ArrayList();
    sampleGroupList.add(new TestBean("1 - childItem1","1 - childItem2"));
    sampleGroupList.add(new TestBean("2 - childItem1","2 - childItem2"));
    sampleGroupList.add(new TestBean("3 - childItem1","3 - childItem2"));

    //add group list
    childWrapper = new ArViewListImpl(sampleGroupList,99);
    childWrapper.setHeaderItem("subTotal",new Float(123.87));
    childWrapperList.add(childWrapper);

    //add group list
    childWrapper = new ArViewListImpl(sampleGroupList,98);
    childWrapper.setHeaderItem("subTotal",new Float(321.87));
    childWrapperList.add(childWrapper);

     //add group list
    childWrapper = new ArViewListImpl(sampleGroupList,97);
    childWrapper.setHeaderItem("subTotal",new Float(333.87));
    childWrapperList.add(childWrapper);

    ArViewListImpl rootWrapper = new ArViewListImpl(childWrapperList,100);
    rootWrapper.setHeaderItem("grandTotal",new Float(8987.98));
    rootWrapper.setHeaderItem("amt1ColTotal",new Float(222.89));
    rootWrapper.setHeaderItem("amt2ColTotal",new Float(333.89));

System.out.println("\n\n*** Java Loop Start ***");
    //display sample data the hard way
    out.println("<h1>Java Test</h1>");
    out.println("<u>root:</u>");
    out.println("<br><b>Total Count: </b>"+ rootWrapper.getTotalCount());
    out.println("<br><b>Amount1 Column Total: </b>"+ rootWrapper.getHeaderItem("amt1ColTotal"));
    out.println("<br><b>Amount2 Column Total: </b>"+ rootWrapper.getHeaderItem("amt2ColTotal"));
    out.println("<br><b>Grand Total: </b>"+ rootWrapper.getHeaderItem("grandTotal"));

    TestBean testBean = null;
    ArViewListImpl tempItem =null;
    ArViewListImpl tempChildWrapper =null;
    Iterator tempChildList = null;
    Iterator tempRootList = rootWrapper.getPageList().iterator();
   while(tempRootList.hasNext()){
        tempItem = (ArViewListImpl)tempRootList.next();
        out.println("<br><u>child</u>:");

        tempChildList = tempItem.getPageList().iterator();
        out.println("<br>Total Count: "+ tempItem.getTotalCount());
        while(tempChildList.hasNext()){
            testBean = (TestBean)tempChildList.next();
             out.println("<br> <b>Test Item1: </b>" + testBean.getItem1() + " <b>Test Item2: </b>" + testBean.getItem2());
        }
        out.println("<br><i><b> Child Wrapper subtotal: </b>" + tempItem.getHeaderItem("subTotal")+"</i>");
    }
    System.out.println("*** Java Loop End ***");

    pageContext.setAttribute("wrapperTestBean",rootWrapper);
%>
<% System.out.println("*** Logic Loop Start ***"); %>
<h1>Logic Test:</H1>
<bean:define name="wrapperTestBean" id="rootWrap" />
<u>root:</u>
<br><b>TotalCount: </b><bean:write name="rootWrap" property="totalCount" />
<br><b>Amt 1 Col Total: </b><bean:write name="rootWrap" property="headerItem(amt1ColTotal)" />
<br><b>Amt 2 Col Total:</b> <bean:write name="rootWrap" property="headerItem(amt2ColTotal)" />
<br><b>Grand Total: </b><bean:write name="rootWrap" property="headerItem(grandTotal)" />
<logic:iterate name="rootWrap" property="pageList" id="childW" >
        <br><u>Child</u>
        <br><b>Total Count: </b><bean:write name="childW" property="totalCount" />
        <logic:iterate name="childW" property="pageList" id="testBeans">
           <br><b>Item1: </b><bean:write name="testBeans" property="item1"/>&nbsp<b>item2: </b><bean:write name="testBeans" property="item2"/>
        </logic:iterate>
        <br><i><b> Child Wrapper subtotal: </b><bean:write  name="childW" property="headerItem(subTotal)"/></i>
</logic:iterate>
<% System.out.println("*** Logic Loop End ***"); %>

<% System.out.println("*** Nested Loop Start ***"); %>
<h1>Nested Test:</H1>
<u>root:</u>
<nested:root name="wrapperTestBean">
<br><b>TotalCount: </b><nested:write property="totalCount" />
<br><b>Amt 1 Col Total: </b><nested:write property="headerItem(amt1ColTotal)" />
<br><b>Amt 2 Col Total:</b> <nested:write property="headerItem(amt2ColTotal)" />
<br><b>Grand Total: </b><nested:write property="headerItem(grandTotal)" />
<br>
    <nested:iterate property="pageList">
        <br><u>Child</u>
        <br><b>Total Count: </b><nested:write property="totalCount" />
        <nested:iterate property="pageList">
           <br><b>Item1: </b><nested:write property="item1"/>&nbsp<b>item2: </b><nested:write property="item2"/>
        </nested:iterate>
        <br><i><b> Child Wrapper subtotal: </b><nested:write property="headerItem(subTotal)"/></i>
    </nested:iterate>

</nested:root>
<% System.out.println("*** Nested Loop End ***"); %>

<%!
public class TestBean implements java.io.Serializable{
    private String item1 = null;
    private String item2 = null;

    public String getItem1(){return this.item1;}
    public String getItem2(){return this.item2;}

    public void setItem1(String i1){this.item1 = i1;}
    public void setItem2(String i2){this.item1 = i2;}

    public TestBean(String i1,String i2){
        this.item1 = i1;
        this.item2 = i2;
    }
}

public class ArViewListImpl{
    private List pageList;
    private int totalCount;
    private Map headerItemMap = new HashMap();

    public ArViewListImpl() {}

    public ArViewListImpl(List list,int totalCount){
        this.pageList = list;
        this.totalCount = totalCount;
    }

    public List getPageList() {
        return this.pageList;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public Object getHeaderItem(String headerItemKey) {
    System.out.println("\ngetHeaderItem: Passed in: "+ headerItemKey + "|Contains Key: "+ this.headerItemMap.containsKey(headerItemKey));
    System.out.println("\t\t|ToString: " + this.toString());

        return this.headerItemMap.get(headerItemKey);
    }

    public void setHeaderItem(String headerItemKey, Object headerItemValue) {
        this.headerItemMap.put(headerItemKey, headerItemValue);
    }

    public String toString(){
        StringBuffer out = new StringBuffer();
        out.append("|totalCount="+this.totalCount);
        out.append("|Headers:").append(this.headerItemMap.toString());
        out.append("|pageList=");
        if(this.pageList == null){
            out.append("NULL");
        }else{
            out.append("|pageList.size()="+this.pageList.size()).append("|");
        }
        return out.toString();
    }
}
%>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>