You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Micael <ca...@harbornet.com> on 2003/04/13 08:29:26 UTC
Examples for Newbies and Question for Oldbies
I get the following readout for a vector iteration with the code which
immediately follows:
READOUT IS:
Vector [com.michaelmcgrady.dao.User@1a6b028,
com.michaelmcgrady.dao.User@772046, com.michaelmcgrady.dao.User@e80317,
com.michaelmcgrady.dao.User@22e3ac, com.michaelmcgrady.dao.User@916ab8,
com.michaelmcgrady.dao.User@f55759]
Next element1 is Aa
Next element2 is
Next element1 is
Next element2 is Aaa
Next element1 is Ab
Next element2 is
Next element1 is
Next element2 is Abb
Next element1 is Ac
Next element2 is
Next element1 is
Next element2 is Acc
Hashtable {c=cc, b=bb, a=aa}
Next element3 is c
Next element4 is cc
Next element3 is b
Next element4 is bb
Next element3 is a
Next element4 is aa
Map {root=com.michaelmcgrady.dao.User@139a0dc,
visitor=com.michaelmcgrady.dao.User@6fd560}
Next element5 is root
Next element5 is com.michaelmcgrady.dao.User@139a0dc
Next element5 is visitor
Next element5 is com.michaelmcgrady.dao.User@6fd560
CODE IS:
<%@ page language='java' %>
<%@ page import='java.util.Hashtable' %>
<%@ page import='java.util.Map' %>
<%@ page import='java.util.Vector' %>
<%@ page import='com.michaelmcgrady.dao.User' %>
<%@ page import='com.michaelmcgrady.dao.UserDAO' %>
<%@ taglib uri='struts-logic' prefix='logic' %>
<%@ taglib uri='struts-bean' prefix='bean' %>
<%@ taglib uri='struts-html' prefix='html' %>
<%
Vector vector = new Vector(89);
vector.add(new User().setName("Aa"));
vector.add(new User().setPassword("Aaa"));
vector.add(new User().setName("Ab"));
vector.add(new User().setPassword("Abb"));
vector.add(new User().setName("Ac"));
vector.add(new User().setPassword("Acc"));
request.setAttribute("aname", vector);
Hashtable hashtable = new Hashtable(89);
hashtable.put("a", "aa");
hashtable.put("b", "bb");
hashtable.put("c", "cc");
request.setAttribute("yourchoice", hashtable);
Map map = UserDAO.getUsers();
request.setAttribute("mymap", map);
%>
<html:html locale='true'>
<body>
<!--
The collection to be iterated over MUST conform to one of the following
requirements in order for iteration to be successful:
An array of Java objects or primitives.
An implementation of java.util.Collection, including ArrayList and Vector.
An implementation of java.util.Enumeration.
An implementation of java.util.Iterator.
An implementation of java.util.Map, including HashMap, Hashtable, and
TreeMap. NOTE - See below for additional information about accessing Maps.
Normally, each object exposed by the iterate tag is an element of the
underlying collection you are iterating over. However, if you iterate over
a Map, the exposed object is of type Map.Entry that has two properties:
key - The key under which this item is stored in the underlying Map.
value - The value that corresponds to this key.
So, if you wish to iterate over the values of a Hashtable, you would
implement code like the following:
-->
Vector <%= vector %><br><br>
<logic:iterate id="options" name="aname">
Next element1 is <bean:write name="options" property="name"/><br>
Next element2 is <bean:write name="options" property="password"/><br>
</logic:iterate>
<br><br>Hashtable <%= hashtable %><br><br>
<logic:iterate id="anything" name="yourchoice">
Next element3 is <bean:write name="anything" property="key"/><br>
Next element4 is <bean:write name="anything" property="value"/><br>
</logic:iterate>
<br><br>Map <%= map %><br><br>
<logic:iterate id="something" name="mymap">
Next element5 is <bean:write name="something" property="key"/><br>
Next element5 is <bean:write name="something" property="value"/><br>
</logic:iterate>
</body>
</html:html>
LEGAL NOTICE
This electronic mail transmission and any accompanying documents contain
information belonging to the sender which may be confidential and legally
privileged. This information is intended only for the use of the
individual or entity to whom this electronic mail transmission was sent as
indicated above. If you are not the intended recipient, any disclosure,
copying, distribution, or action taken in reliance on the contents of the
information contained in this transmission is strictly prohibited. If you
have received this transmission in error, please delete the message. Thank you
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
CODE -- Sigh!
Posted by Micael <ca...@harbornet.com>.
Sorry about the prior mess -- the road to hell, and all that.
ADD OBJECTS TO SCOPE:
request.setAttribute("yourchoice", hashtable);
request.setAttribute("aname", vector);
request.setAttribute("mymap", map);
WRITE VALUES TO JSP PAGE
Vector <%= vector %><br><br>
<logic:iterate id="options" name="aname">
NAME: <bean:write name="options" property="name"/><br>
PASSWORD: <bean:write name="options" property="password"/><br>
</logic:iterate>
<br><br>Hashtable <%= hashtable %><br><br>
<logic:iterate id="anything" name="yourchoice">
Next element3 is <bean:write name="anything" property="key"/><br>
Next element4 is <bean:write name="anything" property="value"/><br>
</logic:iterate>
<br><br>Map <%= map %><br><br>
<logic:iterate id="something" name="mymap">
Next element5 is <bean:write name="something" property="key"/><br>
Next element5 is <bean:write name="something" property="value"/><br>
</logic:iterate>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
THE REST OF THE CODERe:
Examples for Newbies and Question for Oldbies
Posted by Micael <ca...@harbornet.com>.
<%
Vector vector = new Vector(89);
User user1 = new User();
user1.setName("Aa");
user1.setPassword("Aaa");
vector.add(user1);
User user2 = new User();
user2.setName("Aa");
user2.setPassword("Aaa");
vector.add(user2);
User user3 = new User();
user3.setName("Aa");
user3.setPassword("Aaa");
vector.add(user3);
request.setAttribute("aname", vector);
Hashtable hashtable = new Hashtable(89);
hashtable.put("a", "aa");
hashtable.put("b", "bb");
hashtable.put("c", "cc");
request.setAttribute("yourchoice", hashtable);
Map map = UserDAO.getUsers();
request.setAttribute("mymap", map);
%>
<html:html locale='true'>
<body>
Vector <%= vector %><br><br>
<logic:iterate id="options" name="aname">
NAME: <bean:write name="options" property="name"/><br>
PASSWORD: <bean:write name="options" property="password"/><br>
</logic:iterate>
<br><br>Hashtable <%= hashtable %><br><br>
<logic:iterate id="anything" name="yourchoice">
Next element3 is <bean:write name="anything" property="key"/><br>
Next element4 is <bean:write name="anything" property="value"/><br>
</logic:iterate>
<br><br>Map <%= map %><br><br>
<logic:iterate id="something" name="mymap">
Next element5 is <bean:write name="something" property="key"/><br>
Next element5 is <bean:write name="something" property="value"/><br>
</logic:iterate>
</body>
</html:html>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
CODE EXAMPLE: Iterate correct examples for
newbies: Re: Examples for Newbies and Question for
Oldbies
Posted by Micael <ca...@harbornet.com>.
Okay, I got the answer. What a foobar -- I mean foopah. Here is an
example of how it works for <logic:iterator> for newbies. I wish I would
have had this when I started.
<%@ page language='java' %>
<%@ page import='java.util.Hashtable' %>
<%@ page import='java.util.Map' %>
<%@ page import='java.util.Vector' %>
<%@ page import='com.michaelmcgrady.dao.User' %>
<%@ page import='com.michaelmcgrady.dao.UserDAO' %>
<%@ taglib uri='struts-logic' prefix='logic' %>
<%@ taglib uri='struts-bean' prefix='bean' %>
<%@ taglib uri='struts-html' prefix='html' %>
<%
Vector vector = new Vector(89);
User user1 = new User();
user1.setName("Aa");
user1.setPassword("Aaa");
vector.add(user1);
User user2 = new User();
user2.setName("Aa");
user2.setPassword("Aaa");
vector.add(user2);
User user3 = new User();
user3.setName("Aa");
user3.setPassword("Aaa");
vector.add(user3);
request.setAttribute("aname", vector);
Hashtable hashtable = new Hashtable(89);
hashtable.put("a", "aa");
hashtable.put("b", "bb");
hashtable.put("c", "cc");
request.setAttribute("yourchoice", hashtable);
Map map = UserDAO.getUsers();
request.setAttribute("mymap", map);
%>
<html:html locale='true'>
<body>
Vector <%= vector %><br><br>
<logic:iterate id="options" name="aname">
NAME: <bean:write name="options" property="name"/><br>
PASSWORD: <bean:write name="options" property="password"/><br>
</logic:iterate>
<br><br>Hashtable <%= hashtable %><br><br>
<logic:iterate id="anything" name="yourchoice">
Next element3 is <bean:write name="anything" property="key"/><br>
Next element4 is <bean:write name="anything" property="value"/><br>
</logic:iterate>
<br><br>Map <%= map %><br><br>
<logic:iterate id="something" name="mymap">
Next element5 is <bean:write name="something" property="key"/><br>
Next element5 is <bean:write name="something" property="value"/><br>
</logic:iterate>
</body>
</html:html>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: Examples for Newbies and Question for
Oldbies
Posted by Micael <ca...@harbornet.com>.
Forgot to ask my question: Why are there two skips on the vector results?
At 11:29 PM 4/12/03 -0700, you wrote:
>I get the following readout for a vector iteration with the code which
>immediately follows:
>
>READOUT IS:
>
>Vector [com.michaelmcgrady.dao.User@1a6b028,
>com.michaelmcgrady.dao.User@772046, com.michaelmcgrady.dao.User@e80317,
>com.michaelmcgrady.dao.User@22e3ac, com.michaelmcgrady.dao.User@916ab8,
>com.michaelmcgrady.dao.User@f55759]
>
>Next element1 is Aa
>Next element2 is
>Next element1 is
>Next element2 is Aaa
>Next element1 is Ab
>Next element2 is
>Next element1 is
>Next element2 is Abb
>Next element1 is Ac
>Next element2 is
>Next element1 is
>Next element2 is Acc
>
>
>Hashtable {c=cc, b=bb, a=aa}
>
>Next element3 is c
>Next element4 is cc
>Next element3 is b
>Next element4 is bb
>Next element3 is a
>Next element4 is aa
>
>
>Map {root=com.michaelmcgrady.dao.User@139a0dc,
>visitor=com.michaelmcgrady.dao.User@6fd560}
>
>Next element5 is root
>Next element5 is com.michaelmcgrady.dao.User@139a0dc
>Next element5 is visitor
>Next element5 is com.michaelmcgrady.dao.User@6fd560
>
>
>
>CODE IS:
>
>
>
><%@ page language='java' %>
><%@ page import='java.util.Hashtable' %>
><%@ page import='java.util.Map' %>
><%@ page import='java.util.Vector' %>
><%@ page import='com.michaelmcgrady.dao.User' %>
><%@ page import='com.michaelmcgrady.dao.UserDAO' %>
><%@ taglib uri='struts-logic' prefix='logic' %>
><%@ taglib uri='struts-bean' prefix='bean' %>
><%@ taglib uri='struts-html' prefix='html' %>
>
><%
> Vector vector = new Vector(89);
> vector.add(new User().setName("Aa"));
> vector.add(new User().setPassword("Aaa"));
> vector.add(new User().setName("Ab"));
> vector.add(new User().setPassword("Abb"));
> vector.add(new User().setName("Ac"));
> vector.add(new User().setPassword("Acc"));
> request.setAttribute("aname", vector);
>
> Hashtable hashtable = new Hashtable(89);
> hashtable.put("a", "aa");
> hashtable.put("b", "bb");
> hashtable.put("c", "cc");
> request.setAttribute("yourchoice", hashtable);
>
>
> Map map = UserDAO.getUsers();
> request.setAttribute("mymap", map);
>%>
>
>Vector <%= vector %>
>
>Next element1 is
>Next element2 is
>
>
>Hashtable <%= hashtable %>
>
>Next element3 is
>Next element4 is
>
>
>Map <%= map %>
>
>Next element5 is
>Next element5 is
>LEGAL NOTICE This electronic mail transmission and any accompanying
>documents contain information belonging to the sender which may be
>confidential and legally privileged. This information is intended only for
>the use of the individual or entity to whom this electronic mail
>transmission was sent as indicated above. If you are not the intended
>recipient, any disclosure, copying, distribution, or action taken in
>reliance on the contents of the information contained in this transmission
>is strictly prohibited. If you have received this transmission in error,
>please delete the message. Thank you
>--------------------------------------------------------------------- To
>unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org For
>additional commands, e-mail: struts-user-help@jakarta.apache.org
LEGAL NOTICE
This electronic mail transmission and any accompanying documents contain
information belonging to the sender which may be confidential and legally
privileged. This information is intended only for the use of the
individual or entity to whom this electronic mail transmission was sent as
indicated above. If you are not the intended recipient, any disclosure,
copying, distribution, or action taken in reliance on the contents of the
information contained in this transmission is strictly prohibited. If you
have received this transmission in error, please delete the message. Thank
you
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org