The answer, unfortunately, is "all of the above."  The iterate tag iterates
through both maps and collections in any form and in any scope, and you
display the contents of map and collection values with the <bean:write> tag.
A typical map would be key-value pairs placed in memory as
session.setAttribute() objects, whereas typical lists are placed in memory
by the ActionServlet instantiating a JavaBean (as a Struts form bean) and
making it's getters (accessors) and setters (mutators) available to JSPs and
Action classes.

For example, let's say you want to display a list of cities for a particular
state when a user selects a state from a drop-down list in a JSP.  First,
how would you populate the states list?  It would be pretty tedious to
hard-code 50 states + D.C. + any territories (like Puerto Rico or Guam) into
the JSP as HTML <select> <option>s, not to mention bloating the source code.
With Struts, however, this is a piece of cake:

  1.  User is forwarded to the states/cities page from some JSP through some
Action Mapping that uses an Action class that in turn requests a List of
states from a DAO (data access object - a helper class concerned only with
querying the database).

  2.  The Action class takes the List and puts it into application scope (in
this case, since the List can be reused by any other clients):
    ServletConfig.getServletContext().setAttribute( "statesList", states);

  3.  The states/list JSP can then display this List of states as an HTML
drop-down:
    <html:select name="states">
      <logic:terate name="statesList"
                    property="states"
                    scope="application"
                    type="com.company.dao.state"
                    id="state">
        <html:option value="<%= state.getName() %>">
          <%= state.getName() %>
        </html:option>
      </logic:iterate>
    </html:select>

  4.  A JavaScript onchange() event handler can then call the Action class
to request a list of cities from, say the city DAO (City.java), based on the
selected state and populate a "City" drop-down select box in the same
manner.

This is about as simple as it gets, but note that using JSP scripting
variables is generally frowned upon by the purists.  But I hope you get the
idea and then can move on to using the <html:options> tag.  Note that the
List could also have been stored as a TreeMap and you could set the option
values with something like state.getAbbreviation().  The various attributes
are explained in the Struts tag documentation.

To use a JavaBean (Struts form bean - a special use of JavaBeans that use
only booleans and Strings) with a JSP, you simply create a bean with
mutators and accessors matching the names of the input objects in your HTML
form contained in your JSP.  For example, if you have a form taking user
info:

  <html:form action="/do/userInfo" method="post">
    <html:text property="fname" size="20" maxlength="20" />
    <html:text property="lname" size="20" maxlength="20" />
    <html:text property="street" size="20" maxlength="20" />
    <html:text property="city"  size="20" maxlength="20" />
    <html:text property="state" size="20" maxlength="20" />
  </html:form>
  <html:submit value="Submit" />

You would create a form bean like:

***
package and import statements
***

public class UserInfo extends ActionForm implements Serializable {
  private String fname = "";
  private String lname = "";
  private String street = "";
  private String city = "";
  private String state = "";

  public void setFname( String fname) {
    this.fname = fname;
  }
  public String getFname() {
    return fname;
  }
/*
same pattern for other fields
*/

  public void reset( ActionMapping map, HttpServletRequest req) {
    super.reset( map, req);
    fname = "";
    lname = "";
    password = "";
    //etc.
  }
}

Now, when the user hits "Submit," the Action class associated with the path
"/do/userInfo" will be called by ActionServlet but before the action's
execute() method is called, UserInfo's setters will be called and its
properties set, making them available for writing to the database and/or the
JSP the action forwards to in struts-config (Note that if you want the
bean's info available across a user's session, do not reset the properties
in reset() and declare the bean in session scope in struts-config).

Another JSP can now access UserInfo's properties with <bean:write> like:

<bean:write name="UserInfo" property="fname" />
<bean:write name="UserInfo" property="lanem" />
etc.

It's easy to get lost in these tags when you first start out, because they
are very powerful and can do a lot of things - much of which is indocumented
and if discovered by trial-and-error and reading this user list.  The best
thing you can do at this point, however, is thoroughly read the taglib
documentation.

Good luck!
Mark

-----Original Message-----
From: Struts Newsgroup [mailto:[EMAIL PROTECTED]]
Sent: Sunday, June 02, 2002 10:25 AM
To: [EMAIL PROTECTED]
Subject: a newbie question on iterate


Subject: a newbie question on iterate
From: "Gary Tam" <[EMAIL PROTECTED]>
 ===
Hi, I am trying to use iterate to display a collection on a jsp, are there
any simple example out there?  I tried to follow the examples with STRUTS,
but got all confuse.  Do I have to store the collection in a java bean ?  Is
this a collection of java beans or simple value objects? etc...  Can someone
please set me straight please

TIA
Gary



--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to