how to iterate through a map in struts

V

vyshu

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....
 
C

ck

vyshu said:
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.
 
V

vyshu

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..
 
V

vyshu

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
 
L

Lew

vyshu said:
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.
 
T

Tim B

vyshu said:
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>
 
V

vyshu

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.
 
V

vyshu

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.

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

chandan

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
 
T

Tim B

vyshu said:
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.
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.
 
V

vyshu

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- Hide quoted text -

- 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.
 
V

vyshu

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


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


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.- Hide quoted text -

- 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
 
V

vyshu

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,
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top