RE: a newbie question on iterate

2002-06-04 Thread Galbreath, Mark

Good question!  I should have included the "indexId" attribute of
.  JavaScript can capture an onchange event from (in my
example) the states' dropdown and retrieve the associated cities like:


  



  function getCities() {
window.location="/do/getCitiesAction?state=<%=stateIndex%>
  }


In GetCitiesAction.java, you reference the states List that is already in
application (context) scope:

  List states = new ArrayList(( List) context.getAttribute( "statesList");

The state for which you want to find the cities is the element at
"stateIndex:"

  String state = states.get( Integer.valueOf( request.getParameter(
"stateIndex"));

Now ask your DAO for a List of cities for this state and stick them in
session scope so your JSP can access them much the same as you do for the
states:

  request.getSession().setAttribute( "cities", cities);

Have struts config map "success" back to the same JSP page, where now the
cities can be accessed and displayed in a dropdown:


  "
   property="name"
   type="com.company.DAO.City"
   id="city"
   scope="session" />

Now you can populate an  object with option values using "city"
as the name:

  

  


Note that you have this code on in your JSP to begin with, so wrap it in a
 to prevent a NullPointerException when the page is first
rendered.  Also, note that this time I used a  to bring the
cities list into page scope, and the  tag, which handles maps
and collections in one step.

You can get the value any JSP object in JavaScript like:

  
function checkCity() {
  for( var i = 0; i < document.forms[0].cities.length; i++) {
if( document.forms[0].cities.options[ i ].selected == true) {
  var city = document.forms[0].cities.options[ i ].text;

  }
  }
  // do something with the city
}
  

Or manipulate it like:

  
var city = String( <bean:write name="cities" property="name" />);
var population = parseInt( <bean:write name="cities" property="pop" />);
// do something or display
  


There ya go, dude!  Hope I didn't muddy the water too much.
Mark


-Original Message-
From: Gary Tam [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 04, 2002 6:23 AM

Mark, one more question, is it possible to use java script to
acess/maniuplate jsp session objects ?
In your example, the onChange event gets fire and goes out to retrieve the
cities of the state,
how can the action class know which row/state that fired the event ?


Thanks


- Original Message -
From: "Galbreath, Mark" <[EMAIL PROTECTED]>
Newsgroups: Struts
Sent: Monday, June 03, 2002 8:32 AM
Subject: RE: a newbie question on iterate


> 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 
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  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:
> 
>property="states"
> scope="application"
> type="com.company.dao.state"
> id="state">
> 
>   <%= state.getName() %>
> 
>   
> 
>
>   4.  A JavaScript onchange() event handler can then call the Action class
> to request a list of cities f

Re: a newbie question on iterate

2002-06-04 Thread Gary Tam

Mark, one more question, is it possible to use java script to
acess/maniuplate jsp session objects ?
In your example, the onChange event gets fire and goes out to retrieve the
cities of the state,
how can the action class know which row/state that fired the event ?


Thanks


- Original Message -
From: "Galbreath, Mark" <[EMAIL PROTECTED]>
Newsgroups: Struts
Sent: Monday, June 03, 2002 8:32 AM
Subject: RE: a newbie question on iterate


> 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 
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  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:
> 
>property="states"
> scope="application"
> type="com.company.dao.state"
> id="state">
> 
>   <%= state.getName() %>
> 
>   
> 
>
>   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  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:
>
>   
> 
> 
> 
> 
> 
>   
>   
>
> 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).
>
> Anothe

Re: a newbie question on iterate

2002-06-04 Thread Gary Tam

Thanks, you guys are great.


- Original Message -
From: "Galbreath, Mark" <[EMAIL PROTECTED]>
Newsgroups: Struts
Sent: Monday, June 03, 2002 8:32 AM
Subject: RE: a newbie question on iterate


> 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 
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  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:
> 
>property="states"
> scope="application"
> type="com.company.dao.state"
> id="state">
> 
>   <%= state.getName() %>
> 
>   
> 
>
>   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  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:
>
>   
> 
> 
> 
> 
> 
>   
>   
>
> 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  like:
>
> 
> 
> 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
indoc

RE: a newbie question on iterate

2002-06-03 Thread Galbreath, Mark

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  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  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:

  

  <%= state.getName() %>

  


  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  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:

  





  
  

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  like:



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:

For additional commands, e-mail:


--
To unsubscribe, e-mail:   
For additional commands, e-mail: 




Re: a newbie question on iterate

2002-06-02 Thread Tim Sawyer

Use an action to put an object that implements Iterator interface into
session memory, and then in the JSP, your  tag refers to
the name that you put the object in session memory under.

This code will cycle through the objects in the Iterator returned by
usersList.getMyIterator() and run getUserid() on each object returned,
displaying the results from each in a table.  element is a local
variable each time around the loop.  usersList is just a java class in
session memory, under the name "usersList"



  





hth,

Tim.

On Sun, 2002-06-02 at 14:25, Struts Newsgroup wrote:
> 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:   
> For additional commands, e-mail: 
> 



--
To unsubscribe, e-mail:   
For additional commands, e-mail: