how to iterate through a map in struts

Discussion in 'Java' started by vyshu, Apr 9, 2007.

  1. vyshu

    vyshu Guest

    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 code..
    /*public String getId(String key)
    return (String)mapelements(key);
    public Object mapelements(String key)
    Map x=new HashMap();
    return x.get(key);
    now i want to print this in the jsp page where i am fowarding from
    this is my action code..
    LoginForm obj=(LoginForm)form;
    HttpSession session=req.getSession();

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

    please help....
    vyshu, Apr 9, 2007
    1. Advertisements

  2. vyshu

    ck Guest

    Why not set the 'id' in the session scope rather than setting the
    entire form?
    Instead of the above try this


    In jsp, use JSTL to get the Id like this


    Would this make it simpler?

    By the way to access mapelements() method you would need to provide a
    getter method.
    ck, Apr 9, 2007
    1. Advertisements

  3. vyshu

    vyshu Guest

    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, Apr 9, 2007
  4. vyshu

    vyshu Guest

    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
    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........
    vyshu, Apr 9, 2007
  5. vyshu

    Lew Guest

    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, Apr 9, 2007
  6. vyshu

    Tim B Guest

    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"/>
    Tim B, Apr 9, 2007
  7. vyshu

    vyshu Guest

    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 i right?
    <logic:iterate id="mapEntry" name="myMap" >
    here what is this "myMap"? is it just the id of the
    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
    can you please give me the methods of the form bean....
    vyshu, Apr 10, 2007
  8. vyshu

    vyshu Guest

    hi lew,
    I am really sorry.
    I will definitly try to make my questions more readable in future.
    Thankyou for your suggestions.
    vyshu, Apr 10, 2007
  9. vyshu

    chandan Guest


    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, Apr 10, 2007
  10. vyshu

    Tim B Guest

    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
    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.
    Tim B, Apr 10, 2007
  11. vyshu

    vyshu Guest

    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
    ActionForm form;
    LoginForm object=(LoginForm)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
    waiting for your reply.
    vyshu, Apr 10, 2007
  12. vyshu

    vyshu Guest

    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)

    is it so?.
    vyshu, Apr 10, 2007
  13. vyshu

    vyshu Guest

    i got it!!!!
    Thank you so much for helping me.
    vyshu, Apr 10, 2007
  14. vyshu

    vyshu Guest

    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?
    vyshu, Apr 10, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.