Re: a newbie question on iterate
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 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 options, 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
Re: a newbie question on iterate
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 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 options, 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
RE: a newbie question on iterate
Good question! I should have included the indexId attribute of logic:iterate. JavaScript can capture an onchange event from (in my example) the states' dropdown and retrieve the associated cities like: html:select * * * onchange=getCities() logic:iterate * * * indexId=stateIndex * * * /logic:iterate /html:select script type=text/javascript function getCities() { window.location=/do/getCitiesAction?state=%=stateIndex% } /script 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: logic:notEmpty name=cities property=%= session.getAttribute( \cities\) scope=session bean:define name=%= session.getAttribute( \cities\) % property=name type=com.company.DAO.City id=city scope=session / Now you can populate an html:select object with option values using city as the name: html:select property=cities html:options name=city / /html:select /logic:notEmpty Note that you have this code on in your JSP to begin with, so wrap it in a logic:notEmpty to prevent a NullPointerException when the page is first rendered. Also, note that this time I used a bean:define to bring the cities list into page scope, and the html:options tag, which handles maps and collections in one step. You can get the value any JSP object in JavaScript like: script type=text/javascript 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 } /script Or manipulate it like: script type=text/javascript var city = String( bean:write name=cities property=name /); var population = parseInt( bean:write name=cities property=pop /); // do something or display /script 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 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 options, 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
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 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 options, 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
Re: a newbie question on iterate
Use an action to put an object that implements Iterator interface into session memory, and then in the JSP, your logic:iterate 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 table logic:iterate id=element name=usersList property=myIterator tr td width=10% class=databean:write name=element property=userid//td /tr /logic:iterate /table 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: 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]