You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Marc S. Penner" <ma...@yahoo.com> on 2001/06/06 16:32:43 UTC

Nested logic:iterate tags

Is it possible to nest logic:iterate tags?  I would prefer not to try to
spend too much time trying to do something that isn't possible.

Marc


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: Nested logic:iterate tags

Posted by "Marc S. Penner" <ma...@yahoo.com>.
Apparently, it is.  I tried it and it worked quite easily.  Answered my own
question.

Marc

----- Original Message -----
From: "Marc S. Penner" <ma...@yahoo.com>
To: <st...@jakarta.apache.org>
Sent: Wednesday, June 06, 2001 10:32 AM
Subject: Nested logic:iterate tags


> Is it possible to nest logic:iterate tags?  I would prefer not to try to
> spend too much time trying to do something that isn't possible.
>
> Marc
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: Nested logic:iterate tags

Posted by jd...@frii.com.
Marc,
    Here's a sample of nested iterate tags IterateForm.java and
TimeNavDblIter.jsp.
Thanks to Craig et al  it's a piece of cake.


john ware



/////////////////////////////   TimeNavDblIter.jsp



<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<%@ page language="java" import = "com.jdware.beans.*" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>


<%
        IterateForm instance = new IterateForm();
        instance.setName( "yeh baby" );

         pageContext.setAttribute("instance", instance,
PageContext.PAGE_SCOPE);


%>
<html:form action="/time" >


Name: <bean:write name="instance" property='<%= "name" %>' filter="true"
/><br/>
Address  <bean:write name="instance" property='<%= "address" %>'
filter="true" /><br/>
<table border="1" width="100%">
  <tr><th>Type</th><th>Size</th>
    <% int h = 0; %>
    <logic:iterate name="instance" property='<%= "foos[0].bars" %>'
id="anything">
      <th>
        <bean:write name="instance" property='<%= "foos[0].bars[" + h +
"].date" %>' filter="true" />
      </th>
      <% h++; %>
    </logic:iterate>
  </tr>
  <tr>
    <% int i = 0; %>
      <logic:iterate name="instance" property="foos" id="anything">
      <tr>
        <td>
          <bean:write name="instance" property='<%= "foos[" + i + "].type"
%>' filter="true" />
        </td>
        <td>
          <bean:write name="instance" property='<%= "foos[" + i + "].size"
%>' filter="true" />
        </td>

      <% int j = 0; %>
      <logic:iterate name="instance" property='<%= "foos[" + i + "].bars"
%>'  id="anything">
        <td>
          <html:text name="instance" property='<%= "foos[" + i + "].bars[" +
j + "].hours" %>' filter="true" />
        </td>
        <% j++; %>
      </logic:iterate>
      </tr>
    <% i++; %>
    </logic:iterate>

          </table>
           <html:submit/>
        </html:form>
</html:html>

//////////////////////////////////////////////////////////////////  end
TimeNavDblIter.jsp

////////////////////////////// IterateForm.java

package com.jdware.beans;


import java.util.*;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import com.jdware.util.Trace;


public class IterateForm  extends ActionForm {

    public String name = "TestMe";
    public String address = "Sischo Dr";
    public IterateForm(){
        foos = Foo.getFoosByMagic();
    }

    public static class Foo implements Comparable {
        public String type;
        public int size;
        public Foo(){
            type="foo";
            size=0;
            bars = Bar.getBarsByMagic();
        }
        public Foo( String type, int size ){
            bars = Bar.getBarsByMagic();
            this.type = type;
            this.size = size;
        }
        public static ArrayList getFoosByMagic(){
            ArrayList rtn = new ArrayList();
            rtn.add( new Foo("typeA", 42));
            rtn.add( new Foo("typeF", 2));
            rtn.add( new Foo("typeW", 4));
            rtn.add( new Foo("typeH", 0));
            rtn.add( new Foo("typeX", 77));

            return rtn;
        }
        public String getType(){return type;}
        public void setType( String type ){ this.type = type; }

        public int getSize(){ return size; }
        public void setSize( int size ){ this.size = size; }
        public int compareTo(Object o){
            return type.compareTo( ((Foo)o).type );
        }
        public static class Bar implements Comparable{
            /**
             *
             */
            public String date;
            public String getDate(){ return date; }
            public void setDate( String date ){ this.date = date; }
            /**
             *
             */
            public String activity;
            public String getActivity(){ return activity; }
            public void setActivity( String activity ){ this.activity =
activity; }

            /**
             *
             */
            public double hours;
            public double getHours(){ return hours; }
            public void   setHours( double hours ) { this.hours = hours; }

            public Bar(){
                date = "8/5/50";
                activity = "nonsense";
                hours = 8.5;
            }
            public Bar( String date, String activity, double hours ){
                this.date = date;
                this.activity = activity;
                this.hours = hours;
            }
            public static ArrayList getBarsByMagic(){
                ArrayList rtn = new ArrayList();
                rtn.add( new Bar("4/3/54", "chores", 8.5));
                rtn.add( new Bar("2/5/85", "hotguys", 0.5));
                rtn.add( new Bar("6/4/88", "battle", 3.5));
                rtn.add( new Bar("8/5/50", "fun", 18.5));
                return rtn;
            }
            public int compareTo(Object o){
                return date.compareTo( ((Bar)o).date );
            }
            public String toString(){
                StringBuffer buf = new StringBuffer();
                buf.append( " date     : " ).append( date ).append("\n");
                buf.append( " activity : " ).append( activity
).append("\n");
                buf.append( " hours    : " ).append(  hours ).append("\n");
                return buf.toString();
            }
            public String toXML(){
                StringBuffer buf = new StringBuffer();
                buf.append( "<Bar>" ).append("\n");
                buf.append( "  <date>" ).append( date
).append("</date>").append("\n");
                buf.append( "  <activity>" ).append( activity
).append("</activity>").append("\n");
                buf.append( "  <hours>" ).append( hours
).append("</hours>").append("\n");
                buf.append( "</Bar>" ).append("\n");
                return buf.toString();
            }
        } // end Bar
        ArrayList bars;
        public void setBars( ArrayList bars ){ this.bars = bars; }

        public Bar getBars( int index ){
            return (Bar) bars.get( index );
        }
        public void setBars( int index, Bar item ){
            bars.set( index, item );
        }
        public ArrayList getBars(){
            return bars;
        }
          public String toString(){
                StringBuffer buf = new StringBuffer();
                buf.append( "type : " ); buf.append( type ); buf.append(
"\n" );
                buf.append( "size : " ); buf.append( size ); buf.append(
"\n" );
                buf.append( "Bars : " ); buf.append( "\n" );
                ListIterator iter = bars.listIterator();
                Bar currentBar = null;
                while( iter.hasNext() ){
                    currentBar = (Bar) iter.next();
                    buf.append( currentBar.toString() ); buf.append( "\n" );

                }
                return buf.toString();
            }
        public String toXML(){
            StringBuffer buf = new StringBuffer();
            buf.append( "<Foo>" ).append( "\n" );
            buf.append( "  <type>" ).append( type ).append( "  </type>"
).append( "\n" );
            buf.append( "  <size>" ).append( size ).append( "  </size>"
).append( "\n" );

            ListIterator iter = bars.listIterator();
            Bar currentBar = null;
            while( iter.hasNext() ){
                currentBar = (Bar) iter.next();
                buf.append( currentBar.toXML() ).append( "\n" );

            }
            buf.append( "</Foo>" ).append( "\n" );
            return buf.toString();
        }
    } // end Foo

    ArrayList foos;

    public void setFoos( ArrayList foos ){ this.foos = foos; }

    public Foo getFoos( int index ){
        return (Foo) foos.get( index );
    }
    public void setFoos( int index, Foo item ){
        foos.set( index, item );
    }
    public ArrayList getFoos(){
        return foos;
    }

    public String getName(){ return name; }
    public void setName( String name ){ this.name = name; }

    public String getAddress(){ return address; }
    public void   setAddress( String address ){ this.address = address; }
    public String toString(){
        StringBuffer buf = new StringBuffer();
        buf.append( "name    : " ); buf.append( name ); buf.append( "\n" );
        buf.append( "address : " ); buf.append( address ); buf.append( "\n"
);
        buf.append( "Foos    : " ); buf.append( "\n" );
        ListIterator iter = foos.listIterator();
        Foo currentFoo = null;
        while( iter.hasNext() ){
            currentFoo = (Foo) iter.next();
            buf.append( currentFoo.toString() ); buf.append( "\n" );

        }
        return buf.toString();
    }
   public String toXML(){
        StringBuffer buf = new StringBuffer();
        buf.append( "<IterateForm>" ).append( "\n" );
        buf.append( "  <name>" ).append( name ).append( "  </name>"
).append( "\n" );
        buf.append( "  <address>" ).append( address ).append( "  </address>"
).append( "\n" );

        ListIterator iter = foos.listIterator();
        Foo currentFoo = null;
        while( iter.hasNext() ){
            currentFoo = (Foo) iter.next();
            buf.append( currentFoo.toXML() ).append( "\n" );

        }
        buf.append( "</IterateForm>" ).append( "\n" );
        return buf.toString();
    }
   /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        Trace.out( "IterateForm.reset" );
        Trace.out( "This is where the ejbs are called" );

    }


    /**
     * Validate the properties that have been set from this HTTP request,
     * and return an <code>ActionErrors</code> object that encapsulates any
     * validation errors that have been found.  If no errors are found,
return
     * <code>null</code> or an <code>ActionErrors</code> object with no
     * recorded error messages.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        Trace.out( "IterateForm.validate" );
        return new ActionErrors();
    }
    public static void main( String a[] ){
        IterateForm app = new IterateForm();
        System.out.println("IterateForm.toString" );
        System.out.println( app.toString() );
        System.out.println( "Done" );
    }
}



////////////////////////////// End  IterateForm.java



"Marc S. Penner" wrote:

> Is it possible to nest logic:iterate tags?  I would prefer not to try to
> spend too much time trying to do something that isn't possible.
>
> Marc
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com