Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

how to iterate through a map in struts

Thread view: 
vyshu - 09 Apr 2007 07:36 GMT
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....
ck - 09 Apr 2007 11:36 GMT
> "vyshu" <vysh...@gmail.com> wrote:
> i hav a form bean LoginForm.and a getter mathod getID(),which returns
[quoted text clipped - 23 lines]
> 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
vyshu - 09 Apr 2007 12:44 GMT
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..
vyshu - 09 Apr 2007 12:53 GMT
> hi,
> thank you for your reply...but i am very new to struts for
[quoted text clipped - 14 lines]
> 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
Lew - 09 Apr 2007 13:43 GMT
>> hi,
>> thank you for your reply...but i am very new to struts for
[quoted text clipped - 31 lines]
> 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.

Signature

Lew

vyshu - 10 Apr 2007 04:40 GMT
> >> hi,
> >> thank you for your reply...but i am very new to struts for
[quoted text clipped - 53 lines]
>
> - 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
Tim B - 09 Apr 2007 21:46 GMT
> hi,
> i am confused about iterating through a map in struts.
[quoted text clipped - 26 lines]
>
> 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 - 10 Apr 2007 04:37 GMT
> > hi,
> > i am confused about iterating through a map in struts.
[quoted text clipped - 36 lines]
>
> - 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.
Tim B - 10 Apr 2007 05:19 GMT
> > > hi,
> > > i am confused about iterating through a map in struts.
[quoted text clipped - 42 lines]
> 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
[quoted text clipped - 4 lines]
> 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.
vyshu - 10 Apr 2007 05:35 GMT
> > > "vyshu" <vysh...@gmail.com> wrote in message
>
[quoted text clipped - 77 lines]
>
> - Show quoted text -

thanks.
but for the methods getTheMap and setTheMap what shoul be the
parameter and the return values.
public Map getTheMap()
{
return this.theMap;
}
public void setTheMap(Map newMap)
{
this.theMap=newMap;
}

is it so?.
thanks,
vysh
vyshu - 10 Apr 2007 06:04 GMT
Cheers!!!!!!!!!!!
i got it!!!!
Thank you so much for helping me.
vysh.
chandan - 10 Apr 2007 05:07 GMT
> > hi,
> > i am confused about iterating through a map in struts.
[quoted text clipped - 34 lines]
> 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
vyshu - 10 Apr 2007 05:28 GMT
> > "vyshu" <vysh...@gmail.com> wrote in message
>
[quoted text clipped - 59 lines]
>
> - Show quoted text -

hi,
thanks,
ok,i will try to explain what i understood.
In my form bean itself i will declare the HashMap.
//*****************************************************
public class LoginForm extends ActionForm
{
private HashMap themap;
}
//**********************************
Now let me assign the entire form to a request scope in my Action
class.
ActionForm form;
LoginForm object=(LoginForm)form;
request.setAttribute("myRequestScopedBean",form);
//**********************************************

Now can you explain what and all getter methods i need for  acessing
the values of the map, and also where to write it.
and I want to display the values of the map as the options of a select
tag!..
waiting for your reply.
thanks,
vysh.
vyshu - 10 Apr 2007 13:02 GMT
> > hi,
> > i am confused about iterating through a map in struts.
[quoted text clipped - 36 lines]
>
> - Show quoted text -

hi,
now when i am trying with the same code what you have given,its
throwing an exception "Cannot find bean: "mapEntry" in any scope".why
it is so?
thanks,
vysh.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.