Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to iterate through a map in struts

Reply
Thread Tools

how to iterate through a map in struts

 
 
vyshu
Guest
Posts: n/a
 
      04-09-2007
hi,
i am confused about iterating through a map in struts.
i hav a form bean LoginForm.and a getter mathod getID(),which returns
the values of a map if passed the key as parameter....my code..
/*public String getId(String key)
{
return (String)mapelements(key);
}
public Object mapelements(String key)
{
Map x=new HashMap();
x.put("1","vaisahk");
x.put("2","jovin");
x.put("3","praveen");
x.put("4","subhash");
return x.get(key);
}
*/
now i want to print this in the jsp page where i am fowarding from
here..
this is my action code..
LoginForm obj=(LoginForm)form;
sess=obj.getUser();
HttpSession session=req.getSession();
session.setAttribute("usersession",form);

and in the forwarded jsp page i want to print the values of the
map....

please help....

 
Reply With Quote
 
 
 
 
ck
Guest
Posts: n/a
 
      04-09-2007
> "vyshu" <vysh...@gmail.com> wrote:
> i hav a form bean LoginForm.and a getter mathod getID(),which returns
> the values of a map if passed the key as parameter....my code..
> /*public String getId(String key)
> {
> return (String)mapelements(key);
> }
> public Object mapelements(String key)
> {
> Map x=new HashMap();
> x.put("1","vaisahk");
> x.put("2","jovin");
> x.put("3","praveen");
> x.put("4","subhash");
> return x.get(key);
> }
> */
> now i want to print this in the jsp page where i am fowarding from
> here..
> this is my action code..
> LoginForm obj=(LoginForm)form;
> sess=obj.getUser();
> HttpSession session=req.getSession();
> session.setAttribute("usersession",form);
>
> and in the forwarded jsp page i want to print the values of the
> map....


Why not set the 'id' in the session scope rather than setting the
entire form?

> HttpSession session=req.getSession();
> session.setAttribute("usersession",form);


Instead of the above try this

session.setAttribute("userId",form.getId(key));

In jsp, use JSTL to get the Id like this

${userId}

Would this make it simpler?

By the way to access mapelements() method you would need to provide a
getter method.

--
Ck
http://www.gfour.net

 
Reply With Quote
 
 
 
 
vyshu
Guest
Posts: n/a
 
      04-09-2007
hi,
thank you for your reply...but i am very new to struts for
understanding the exact thing.....
i will try to explain you what i hav understood so far..
from the bigining.......
in the application what i am trying to build...there is be a login
form which accepts the user id and password....i hav written the
formbean...its LoginForm which has the setter and getter methods of
the form properties....
now i want to extract customer id's from the database and display it
in the next jsp page....
for that actually i was trying with the demo values.......
if in the same LoginForm i assign the getter method for the map will
it give any errors.....lots of confusions!!!!!!!
it would be really helpful if u can explain me the entire stuff from
assigning the values to the map....writing the getter and setter
methods...and displaying it in the form using..a select option!!....
........plz help..

 
Reply With Quote
 
vyshu
Guest
Posts: n/a
 
      04-09-2007
On Apr 9, 4:44 pm, "vyshu" <vysh...@gmail.com> wrote:
> hi,
> thank you for your reply...but i am very new to struts for
> understanding the exact thing.....
> i will try to explain you what i hav understood so far..
> from the bigining.......
> in the application what i am trying to build...there is be a login
> form which accepts the user id and password....i hav written the
> formbean...its LoginForm which has the setter and getter methods of
> the form properties....
> now i want to extract customer id's from the database and display it
> in the next jsp page....
> for that actually i was trying with the demo values.......
> if in the same LoginForm i assign the getter method for the map will
> it give any errors.....lots of confusions!!!!!!!
> it would be really helpful if u can explain me the entire stuff from
> assigning the values to the map....writing the getter and setter
> methods...and displaying it in the form using..a select option!!....
> .......plz help..


and on the process i was just trying to just display the values on the
jsp page...
i just declared a getId() method in the LoginForm which returns a
string value if passed the key.....
assigned the complete bean to a session...and callled in the jsp
page.......
for(int i=1;i<3;i++)
{
String nam="id("+i+")";%>
<html:text name="usersession" property="<%=nam%>"/>
<%}%>
it displayed can i do something similar in the other case also,where i
want the data's inside a select........
........................??????????
thanks,
vysh

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-09-2007
vyshu wrote:
> On Apr 9, 4:44 pm, "vyshu" <vysh...@gmail.com> wrote:
>> hi,
>> thank you for your reply...but i am very new to struts for
>> understanding the exact thing.....
>> i will try to explain you what i hav understood so far..
>> from the bigining.......
>> in the application what i am trying to build...there is be a login
>> form which accepts the user id and password....i hav written the
>> formbean...its LoginForm which has the setter and getter methods of
>> the form properties....
>> now i want to extract customer id's from the database and display it
>> in the next jsp page....
>> for that actually i was trying with the demo values.......
>> if in the same LoginForm i assign the getter method for the map will
>> it give any errors.....lots of confusions!!!!!!!
>> it would be really helpful if u can explain me the entire stuff from
>> assigning the values to the map....writing the getter and setter
>> methods...and displaying it in the form using..a select option!!....
>> .......plz help..

>
> and on the process i was just trying to just display the values on the
> jsp page...
> i just declared a getId() method in the LoginForm which returns a
> string value if passed the key.....
> assigned the complete bean to a session...and callled in the jsp
> page.......
> for(int i=1;i<3;i++)
> {
> String nam="id("+i+")";%>
> <html:text name="usersession" property="<%=nam%>"/>
> <%}%>
> it displayed can i do something similar in the other case also,where i
> want the data's inside a select........
> .......................??????????
> thanks,
> vysh


Just a side note, not to answer your particular question but to help you get
better answers and help everyone else to better understand your questions.

When you post in English there are grammar rules (conventions) to make the
post more readable. A sentence begins with an upper-case letter and ends with
a single period, not a series of ellipses. More than one question mark or
exclamation point in a row is redundant. The word "I" is spelled with an
upper-case letter. The word "you" is spelled with three letters. The word
"please" does not have a "z" in it and is spelled with six letters. There
should be two spaces between sentences.

When you post code it should be indented similarly to the Sun Java coding
conventions. It also helps to post an SSCCE.

The way you posted your messages the sentences are all run together; there is
no white space or change of letter case to aid readability.

--
Lew
 
Reply With Quote
 
Tim B
Guest
Posts: n/a
 
      04-09-2007

"vyshu" <> wrote in message
news: oups.com...
> hi,
> i am confused about iterating through a map in struts.
> i hav a form bean LoginForm.and a getter mathod getID(),which returns
> the values of a map if passed the key as parameter....my code..
> /*public String getId(String key)
> {
> return (String)mapelements(key);
> }
> public Object mapelements(String key)
> {
> Map x=new HashMap();
> x.put("1","vaisahk");
> x.put("2","jovin");
> x.put("3","praveen");
> x.put("4","subhash");
> return x.get(key);
> }
> */
> now i want to print this in the jsp page where i am fowarding from
> here..
> this is my action code..
> LoginForm obj=(LoginForm)form;
> sess=obj.getUser();
> HttpSession session=req.getSession();
> session.setAttribute("usersession",form);
>
> and in the forwarded jsp page i want to print the values of the
> map....
>
> please help....
>


Here's an example using Struts tags.

<bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
<logic:iterate id="mapEntry" name="myMap" >
<bean:write name="mapEntry" property="key"/> <bean:write
name="mapEntry" property="value"/>
</logic:iterate>


 
Reply With Quote
 
vyshu
Guest
Posts: n/a
 
      04-10-2007
On Apr 10, 1:46 am, "Tim B" <nos...@someisp.ca> wrote:
> "vyshu" <vysh...@gmail.com> wrote in message
>
> news: oups.com...
>
>
>
>
>
> > hi,
> > i am confused about iterating through a map in struts.
> > i hav a form bean LoginForm.and a getter mathod getID(),which returns
> > the values of a map if passed the key as parameter....my code..
> > /*public String getId(String key)
> > {
> > return (String)mapelements(key);
> > }
> > public Object mapelements(String key)
> > {
> > Map x=new HashMap();
> > x.put("1","vaisahk");
> > x.put("2","jovin");
> > x.put("3","praveen");
> > x.put("4","subhash");
> > return x.get(key);
> > }
> > */
> > now i want to print this in the jsp page where i am fowarding from
> > here..
> > this is my action code..
> > LoginForm obj=(LoginForm)form;
> > sess=obj.getUser();
> > HttpSession session=req.getSession();
> > session.setAttribute("usersession",form);

>
> > and in the forwarded jsp page i want to print the values of the
> > map....

>
> > please help....

>
> Here's an example using Struts tags.
>
> <bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
> <logic:iterate id="mapEntry" name="myMap" >
> <bean:write name="mapEntry" property="key"/> <bean:write
> name="mapEntry" property="value"/>
> </logic:iterate>- Hide quoted text -
>
> - Show quoted text -


hi,
Thank you for your suggestions.but i have some questions.
<bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
here we are defining the bean in request scope and the map name,which
is there inside it..am i right?
<logic:iterate id="mapEntry" name="myMap" >
here what is this "myMap"? is it just the id of the
myRequestScopedbean?
> <bean:write name="mapEntry" property="key"/> <bean:write

here mapEntry points to the id of the iterate tag....but what is the
property key?, and also what is the value of the next line.
my doubt is that,do we have to write the getter methods for
"theMap","key","value"...
can you please give me the methods of the form bean....
thanks,
vysh.

 
Reply With Quote
 
vyshu
Guest
Posts: n/a
 
      04-10-2007
On Apr 9, 5:43 pm, Lew <l...@nospam.lewscanon.com> wrote:
> vyshu wrote:
> > On Apr 9, 4:44 pm, "vyshu" <vysh...@gmail.com> wrote:
> >> hi,
> >> thank you for your reply...but i am very new to struts for
> >> understanding the exact thing.....
> >> i will try to explain you what i hav understood so far..
> >> from the bigining.......
> >> in the application what i am trying to build...there is be a login
> >> form which accepts the user id and password....i hav written the
> >> formbean...its LoginForm which has the setter and getter methods of
> >> the form properties....
> >> now i want to extract customer id's from the database and display it
> >> in the next jsp page....
> >> for that actually i was trying with the demo values.......
> >> if in the same LoginForm i assign the getter method for the map will
> >> it give any errors.....lots of confusions!!!!!!!
> >> it would be really helpful if u can explain me the entire stuff from
> >> assigning the values to the map....writing the getter and setter
> >> methods...and displaying it in the form using..a select option!!....
> >> .......plz help..

>
> > and on the process i was just trying to just display the values on the
> > jsp page...
> > i just declared a getId() method in the LoginForm which returns a
> > string value if passed the key.....
> > assigned the complete bean to a session...and callled in the jsp
> > page.......
> > for(int i=1;i<3;i++)
> > {
> > String nam="id("+i+")";%>
> > <html:text name="usersession" property="<%=nam%>"/>
> > <%}%>
> > it displayed can i do something similar in the other case also,where i
> > want the data's inside a select........
> > .......................??????????
> > thanks,
> > vysh

>
> Just a side note, not to answer your particular question but to help you get
> better answers and help everyone else to better understand your questions.
>
> When you post in English there are grammar rules (conventions) to make the
> post more readable. A sentence begins with an upper-case letter and ends with
> a single period, not a series of ellipses. More than one question mark or
> exclamation point in a row is redundant. The word "I" is spelled with an
> upper-case letter. The word "you" is spelled with three letters. The word
> "please" does not have a "z" in it and is spelled with six letters. There
> should be two spaces between sentences.
>
> When you post code it should be indented similarly to the Sun Java coding
> conventions. It also helps to post an SSCCE.
>
> The way you posted your messages the sentences are all run together; there is
> no white space or change of letter case to aid readability.
>
> --
> Lew- Hide quoted text -
>
> - Show quoted text -


hi lew,
I am really sorry.
I will definitly try to make my questions more readable in future.
Thankyou for your suggestions.
thanks,
vysh

 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      04-10-2007
On Apr 10, 1:46 am, "Tim B" <nos...@someisp.ca> wrote:
> "vyshu" <vysh...@gmail.com> wrote in message
>
> news: oups.com...
>
>
>
> > hi,
> > i am confused about iterating through a map in struts.
> > i hav a form bean LoginForm.and a getter mathod getID(),which returns
> > the values of a map if passed the key as parameter....my code..
> > /*public String getId(String key)
> > {
> > return (String)mapelements(key);
> > }
> > public Object mapelements(String key)
> > {
> > Map x=new HashMap();
> > x.put("1","vaisahk");
> > x.put("2","jovin");
> > x.put("3","praveen");
> > x.put("4","subhash");
> > return x.get(key);
> > }
> > */
> > now i want to print this in the jsp page where i am fowarding from
> > here..
> > this is my action code..
> > LoginForm obj=(LoginForm)form;
> > sess=obj.getUser();
> > HttpSession session=req.getSession();
> > session.setAttribute("usersession",form);

>
> > and in the forwarded jsp page i want to print the values of the
> > map....

>
> > please help....

>
> Here's an example using Struts tags.
>
> <bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
> <logic:iterate id="mapEntry" name="myMap" >
> <bean:write name="mapEntry" property="key"/> <bean:write
> name="mapEntry" property="value"/>
> </logic:iterate>


Vyshu,

Just elaborating above example for better understanding.
<bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>

1. myRequestScopedBean- is the bean class(DTO/VO's) in which you will
be decalaring your HashMap as property.
2. theMap- is the name of HashMap, like private HashMap theMap in the
bean class, which you will be populating in Action/impl classes.
3. myMap- is just a reference variable with which this HashMap will be
refered within this scope.

<logic:iterate id="mapEntry" name="myMap" >
Now as you have hashmap reference so you iterate over it, giving it a
id for next scope in which you loop/iterated within the entry and get
its keys and values printed out.

-Chandan

 
Reply With Quote
 
Tim B
Guest
Posts: n/a
 
      04-10-2007

"vyshu" <> wrote in message
news: oups.com...
> On Apr 10, 1:46 am, "Tim B" <nos...@someisp.ca> wrote:
> > "vyshu" <vysh...@gmail.com> wrote in message
> >
> > news: oups.com...
> >
> >
> >
> >
> >
> > > hi,
> > > i am confused about iterating through a map in struts.
> > > i hav a form bean LoginForm.and a getter mathod getID(),which returns
> > > the values of a map if passed the key as parameter....my code..
> > > /*public String getId(String key)
> > > {
> > > return (String)mapelements(key);
> > > }
> > > public Object mapelements(String key)
> > > {
> > > Map x=new HashMap();
> > > x.put("1","vaisahk");
> > > x.put("2","jovin");
> > > x.put("3","praveen");
> > > x.put("4","subhash");
> > > return x.get(key);
> > > }
> > > */
> > > now i want to print this in the jsp page where i am fowarding from
> > > here..
> > > this is my action code..
> > > LoginForm obj=(LoginForm)form;
> > > sess=obj.getUser();
> > > HttpSession session=req.getSession();
> > > session.setAttribute("usersession",form);

> >
> > > and in the forwarded jsp page i want to print the values of the
> > > map....

> >
> > > please help....

> >
> > Here's an example using Struts tags.
> >
> > <bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
> > <logic:iterate id="mapEntry" name="myMap" >
> > <bean:write name="mapEntry" property="key"/> <bean:write
> > name="mapEntry" property="value"/>
> > </logic:iterate>- Hide quoted text -
> >
> > - Show quoted text -

>
> hi,
> Thank you for your suggestions.but i have some questions.
> <bean:define name="myRequestScopedBean" property="theMap" id="myMap"/>
> here we are defining the bean in request scope and the map name,which
> is there inside it..am i right?


Actually we are finding the bean in request scope. If it's not there,an
exception will be thrown. The bean (form bean or whatever) has a property
named "theMap" which is a java.util.Map

with id="myMap" we are just giving this map a local (to the jsp) name so
we can refer to it later in the logic:iterate tag

> <logic:iterate id="mapEntry" name="myMap" >
> here what is this "myMap"? is it just the id of the
> myRequestScopedbean?


not the id of myRequestScopedBean, but the id of the Map contained in
myRequestScopedBean.

> > <bean:write name="mapEntry" property="key"/> <bean:write

> here mapEntry points to the id of the iterate tag....but what is the
> property key?, and also what is the value of the next line.
> my doubt is that,do we have to write the getter methods for
> "theMap","key","value"...
> can you please give me the methods of the form bean....
> thanks,
> vysh.
>

if theMap is a java.util.Map, the Struts tag will take care of accessing
the keys and values of the Map for you. You must literally use
property="key" and property="value". You do not have to write any getters
and setters other than those in the form bean for "theMap". These will be
in standard bean format - getTheMap and setTheMap.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Iterate through an empty STL Map Suneeel@gmail.com C++ 3 09-13-2006 01:31 PM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
Iterate through a hastable in struts thenrick Java 0 09-23-2003 03:39 PM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57