You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Melissa <ed...@jamethial.ath.cx> on 2001/07/24 16:51:28 UTC

Another question about the logic:iterate tag

Hi again.  I am wondering whether the logic:iterate tag requires you to use
a bean (or bean property) as the collection to iterate over. I would
actually like to iterate over a hashmap that i placed in the request in my
action class, but I have not determined through the user docs whether this
is possible or not.

Any help would be greatly appreciated!

Melissa


RE: Another question about the logic:iterate tag

Posted by Melissa <ed...@jamethial.ath.cx>.
Yes - yes, and again, yes. Thanks, Ted.

To further expound (in the chance that it might help someone else), when one
needs to iterate through the key/value pairs of a hashmap, instantiate the
object as type Map:

Map aMap = new HasMap();

and place it in the request.  When you need to retrieve the elements, you
can save time by using the struts logic:iterate tag as follows:
<logic:iterate name="map" id="mapEntry" scope="request">
	<bean:write name="mapEntry" property="key"/>
	<bean:write name="mapEntry" property="value"/>
</logic:iterate>

This works b/c the element that gets exposed as the iterator is actually a
Map.Entry object, which contains the key value pairs as properties (although
this utility is only available during the life of the iteration).

Very useful! It reduced my code from a natty 11 lines (see below) down to 4.

Feeling *much* better now,
Melissa


-----Original Message-----
From: Ted Husted [mailto:husted@apache.org]
Sent: Tuesday, July 24, 2001 1:35 PM
To: struts-user@jakarta.apache.org
Subject: Re: Another question about the logic:iterate tag


id is the name you want to give each item in the map as it iterates.

For more see, <
http://jakarta.apache.org/struts/struts-logic.html#iterate >

We are about to add more indexing support, which may help you further.

See the Indexed Tablibs item at

< http://husted.com/about/struts/resources.htm#taglibs >

for an early release.

Melissa wrote:
>
> I have just tried this, but the "id" attribute is apparently required. My
> guess is that this can just be an arbitrary variable... However, the
bigger
> issue seems to be that the iterate tag isn't really suited to the
retrieval
> of more than one oject (i.e. the hashmap's key/value pairs)...
>
> here's the old code I was hoping to "convert":
> <!--%           HashMap jmap = (HashMap)request.getAttribute("hmap");
>                 for (Iterator i = jmap.keySet().iterator(); i.hasNext(); )
{
>                         String key = (String)i.next();
>                         Object obj = jmap.get(key);
> %>
>                                 <tr>
>                                         <td><font size=1><%= key
%></font></td>
>                                         <td><font size=1><%=
obj.toString()%></font></td>
>                                 </tr-->
> <%
>         }
> %>
>
> Is there a way to integrate this into a Struts iterate tag, or should i
just
> stick with the original code?
>
> Thanks so much for your help.
> Melissa
> (Strutting better every day)
>
> -----Original Message-----
> From: Ted Husted [mailto:husted@apache.org]
> Sent: Tuesday, July 24, 2001 11:03 AM
> To: struts-user@jakarta.apache.org
> Subject: Re: Another question about the logic:iterate tag
>
> The docs say "JSP bean", but the meaning there is broad. So, yes, you
> can do this:
>
> request.setAttribute("hashmap",hashmap);
>
> ...
>
> <logic:iterate name="hashmap">
>
> To use an actual bean, you would also specify the property
>
> <logic:iterate name="javaBean" property="hashmap">
>
> which resolves to javaBean.getHashmap()
>
> Melissa wrote:
> >
> > Hi again.  I am wondering whether the logic:iterate tag requires you to
> use
> > a bean (or bean property) as the collection to iterate over. I would
> > actually like to iterate over a hashmap that i placed in the request in
my
> > action class, but I have not determined through the user docs whether
this
> > is possible or not.
> >
> > Any help would be greatly appreciated!
> >
> > Melissa
>
> -- Ted Husted, Husted dot Com, Fairport NY USA.
> -- Custom Software ~ Technical Services.
> -- Tel 716 737-3463.
> -- http://www.husted.com/about/struts/

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/


Re: Another question about the logic:iterate tag

Posted by Ted Husted <hu...@apache.org>.
id is the name you want to give each item in the map as it iterates. 

For more see, <
http://jakarta.apache.org/struts/struts-logic.html#iterate >

We are about to add more indexing support, which may help you further.

See the Indexed Tablibs item at 

< http://husted.com/about/struts/resources.htm#taglibs >

for an early release. 

Melissa wrote:
> 
> I have just tried this, but the "id" attribute is apparently required. My
> guess is that this can just be an arbitrary variable... However, the bigger
> issue seems to be that the iterate tag isn't really suited to the retrieval
> of more than one oject (i.e. the hashmap's key/value pairs)...
> 
> here's the old code I was hoping to "convert":
> <!--%           HashMap jmap = (HashMap)request.getAttribute("hmap");
>                 for (Iterator i = jmap.keySet().iterator(); i.hasNext(); ) {
>                         String key = (String)i.next();
>                         Object obj = jmap.get(key);
> %>
>                                 <tr>
>                                         <td><font size=1><%= key %></font></td>
>                                         <td><font size=1><%= obj.toString()%></font></td>
>                                 </tr-->
> <%
>         }
> %>
> 
> Is there a way to integrate this into a Struts iterate tag, or should i just
> stick with the original code?
> 
> Thanks so much for your help.
> Melissa
> (Strutting better every day)
> 
> -----Original Message-----
> From: Ted Husted [mailto:husted@apache.org]
> Sent: Tuesday, July 24, 2001 11:03 AM
> To: struts-user@jakarta.apache.org
> Subject: Re: Another question about the logic:iterate tag
> 
> The docs say "JSP bean", but the meaning there is broad. So, yes, you
> can do this:
> 
> request.setAttribute("hashmap",hashmap);
> 
> ...
> 
> <logic:iterate name="hashmap">
> 
> To use an actual bean, you would also specify the property
> 
> <logic:iterate name="javaBean" property="hashmap">
> 
> which resolves to javaBean.getHashmap()
> 
> Melissa wrote:
> >
> > Hi again.  I am wondering whether the logic:iterate tag requires you to
> use
> > a bean (or bean property) as the collection to iterate over. I would
> > actually like to iterate over a hashmap that i placed in the request in my
> > action class, but I have not determined through the user docs whether this
> > is possible or not.
> >
> > Any help would be greatly appreciated!
> >
> > Melissa
> 
> -- Ted Husted, Husted dot Com, Fairport NY USA.
> -- Custom Software ~ Technical Services.
> -- Tel 716 737-3463.
> -- http://www.husted.com/about/struts/

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/

Re: & scripting variable

Posted by Ted Husted <hu...@apache.org>.
The best thing would be to submit a patch to Bugzilla as an enhancement
request and a use-case to DEV to convince someone to commit the patch
;-)

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/


Erik Hatcher wrote:
> 
> No arguments here.   It certainly could.... and it would be a fairly easy
> modification for it to do so.
> 
> Perhaps you could post a use-case for why you need it.   If you convinced me
> enough I'd make the changes and submit them as a patch for consideration by
> the struts-dev group.  Or you could make the changes yourself.
> 
>     Erik

Re: & scripting variable

Posted by Gregor Rayman <gr...@gmx.net>.
"Erik Hatcher" <er...@earthlink.net> wrote:

> > But I still can access the form bean in scriplet, only the scriplet is
> > more complicated and error prone. And when so many other structs tags
> > define scriplet varibales, why should not <html:form> do it too?
>
> No arguments here.   It certainly could.... and it would be a fairly easy
> modification for it to do so.
>
> Perhaps you could post a use-case for why you need it.   If you convinced
me
> enough I'd make the changes and submit them as a patch for consideration
by
> the struts-dev group.  Or you could make the changes yourself.
>
>     Erik

OK. I will do it tomorrow. But I am quite new to struts, and have no
experience
with sumbitting code to open source projects, I just will send the modified
code to you, if you agree.

And if I don't convice you, I'll be happy to learn more strutsy way to do
the
thing :-)

--
gR


Re: & scripting variable

Posted by Erik Hatcher <er...@earthlink.net>.
> > > Now I have to either covert the attribute to local variable or
> > > use <bean:define>. Why not just create the variable like <bean:define>
> > > does?
> >
> > But one of the main purposes of Struts is to get away from using
> scriptlets.
>
> I agree. There are tasks where no scriplets are necessary so they should
> not be used. Sometime however avoiding a scriplet is not easy, and writing
> custom tags with the sole purpose to avoid one scriplet in one jsp sounds
> like overkill.
>
> But I still can access the form bean in scriplet, only the scriplet is
> more complicated and error prone. And when so many other structs tags
define
> scriplet varibales, why should not <html:form> do it too?

No arguments here.   It certainly could.... and it would be a fairly easy
modification for it to do so.

Perhaps you could post a use-case for why you need it.   If you convinced me
enough I'd make the changes and submit them as a patch for consideration by
the struts-dev group.  Or you could make the changes yourself.

    Erik



Re: & scripting variable

Posted by Gregor Rayman <gr...@gmx.net>.
"Erik Hatcher" <er...@earthlink.net> wrote:


> > Perhaps. But sometimes I just want to use the form in a scriplet.
>
> Perhaps?   Well, it DOES create a page scoped attribute - no perhaps about
> it! :)   I checked the source, FormTag.java.

OK, ok :-)


> > Now I have to either covert the attribute to local variable or
> > use <bean:define>. Why not just create the variable like <bean:define>
> > does?
>
> But one of the main purposes of Struts is to get away from using
scriptlets.

I agree. There are tasks where no scriplets are necessary so they should
not be used. Sometime however avoiding a scriplet is not easy, and writing
custom tags with the sole purpose to avoid one scriplet in one jsp sounds
like overkill.

But I still can access the form bean in scriplet, only the scriplet is
more complicated and error prone. And when so many other structs tags define
scriplet varibales, why should not <html:form> do it too?

--
gR


Re: & scripting variable

Posted by Erik Hatcher <er...@earthlink.net>.
> Perhaps. But sometimes I just want to use the form in a scriplet.

Perhaps?   Well, it DOES create a page scoped attribute - no perhaps about
it! :)   I checked the source, FormTag.java.

> Now I have to either covert the attribute to local variable or
> use <bean:define>. Why not just create the variable like <bean:define>
> does?

But one of the main purposes of Struts is to get away from using scriptlets.


----- Original Message -----
From: "Erik Hatcher" <er...@earthlink.net>
To: <st...@jakarta.apache.org>
Sent: Tuesday, July 24, 2001 7:25 PM
Subject: Re: <html:form> & scripting variable


> It does set a page scoped attribute (keyed by the form name from the
> mapping) that contains the form object.
>
>     Erik
>
>
> ----- Original Message -----
> From: "Gregor Rayman" <gr...@gmx.net>
> To: <st...@jakarta.apache.org>
> Sent: Tuesday, July 24, 2001 9:50 AM
> Subject: <html:form> & scripting variable
>
>
> > Hi,
> >
> > why does <html:form> generate no scripting variable? Would be nice, if
> > it did.
> >
> > --
> > gR
> >
>



Re: & scripting variable

Posted by Gregor Rayman <gr...@gmx.net>.
Perhaps. But sometimes I just want to use the form in a scriplet.

Now I have to either covert the attribute to local variable or
use <bean:define>. Why not just create the variable like <bean:define>
does?

--
gR


----- Original Message ----- 
From: "Erik Hatcher" <er...@earthlink.net>
To: <st...@jakarta.apache.org>
Sent: Tuesday, July 24, 2001 7:25 PM
Subject: Re: <html:form> & scripting variable


> It does set a page scoped attribute (keyed by the form name from the
> mapping) that contains the form object.
> 
>     Erik
> 
> 
> ----- Original Message -----
> From: "Gregor Rayman" <gr...@gmx.net>
> To: <st...@jakarta.apache.org>
> Sent: Tuesday, July 24, 2001 9:50 AM
> Subject: <html:form> & scripting variable
> 
> 
> > Hi,
> >
> > why does <html:form> generate no scripting variable? Would be nice, if
> > it did.
> >
> > --
> > gR
> >
> 


Re: & scripting variable

Posted by Erik Hatcher <er...@earthlink.net>.
It does set a page scoped attribute (keyed by the form name from the
mapping) that contains the form object.

    Erik


----- Original Message -----
From: "Gregor Rayman" <gr...@gmx.net>
To: <st...@jakarta.apache.org>
Sent: Tuesday, July 24, 2001 9:50 AM
Subject: <html:form> & scripting variable


> Hi,
>
> why does <html:form> generate no scripting variable? Would be nice, if
> it did.
>
> --
> gR
>


& scripting variable

Posted by Gregor Rayman <gr...@gmx.net>.
Hi,

why does <html:form> generate no scripting variable? Would be nice, if 
it did.

--
gR


RE: Another question about the logic:iterate tag

Posted by Melissa <ed...@jamethial.ath.cx>.
I have just tried this, but the "id" attribute is apparently required. My
guess is that this can just be an arbitrary variable... However, the bigger
issue seems to be that the iterate tag isn't really suited to the retrieval
of more than one oject (i.e. the hashmap's key/value pairs)...

here's the old code I was hoping to "convert":
<!--%		HashMap jmap = (HashMap)request.getAttribute("hmap");
		for (Iterator i = jmap.keySet().iterator(); i.hasNext(); ) {
			String key = (String)i.next();
			Object obj = jmap.get(key);
%>
				<tr>
					<td><font size=1><%= key %></font></td>
					<td><font size=1><%= obj.toString()%></font></td>
				</tr-->
<%
	}
%>

Is there a way to integrate this into a Struts iterate tag, or should i just
stick with the original code?


Thanks so much for your help.
Melissa
(Strutting better every day)

-----Original Message-----
From: Ted Husted [mailto:husted@apache.org]
Sent: Tuesday, July 24, 2001 11:03 AM
To: struts-user@jakarta.apache.org
Subject: Re: Another question about the logic:iterate tag


The docs say "JSP bean", but the meaning there is broad. So, yes, you
can do this:

request.setAttribute("hashmap",hashmap);

...

<logic:iterate name="hashmap">

To use an actual bean, you would also specify the property

<logic:iterate name="javaBean" property="hashmap">

which resolves to javaBean.getHashmap()

Melissa wrote:
>
> Hi again.  I am wondering whether the logic:iterate tag requires you to
use
> a bean (or bean property) as the collection to iterate over. I would
> actually like to iterate over a hashmap that i placed in the request in my
> action class, but I have not determined through the user docs whether this
> is possible or not.
>
> Any help would be greatly appreciated!
>
> Melissa

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/


Re: Another question about the logic:iterate tag

Posted by Ted Husted <hu...@apache.org>.
The docs say "JSP bean", but the meaning there is broad. So, yes, you
can do this:

request.setAttribute("hashmap",hashmap);

...

<logic:iterate name="hashmap">

To use an actual bean, you would also specify the property

<logic:iterate name="javaBean" property="hashmap">

which resolves to javaBean.getHashmap()

Melissa wrote:
> 
> Hi again.  I am wondering whether the logic:iterate tag requires you to use
> a bean (or bean property) as the collection to iterate over. I would
> actually like to iterate over a hashmap that i placed in the request in my
> action class, but I have not determined through the user docs whether this
> is possible or not.
> 
> Any help would be greatly appreciated!
> 
> Melissa

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/