Inah
Inah

Reputation: 41

How to populate the list in an s:select tags using hashmap?

Can you help me with the jsp that I'm doing? What I need to come up is to have my dropdown list using s:select in struts2 to have list values that will be coming from a hashmap in an action.

I haven't read anybody to answer this question properly when I read some of the forums asking this same question.

in my Action class, I have this hashmap:

    private HashMap<String, String> nationalities ;

     public HashMap<String, String> getNationalities () {
    return nationalities ;
}

public void setNationalities(HashMap<String, String> nationalities) {
    this.nationalities = nationalities;
}

   public String execute(){
    nationalities = new HashMap<String, String>();
    nationalities .put("A","American");
    nationalities .put("B","Canadian");

    return success;
   }

.. Please help on how can I properly call these values to be mapped in my jsp?

Thanks a lot.. hope you could give me an answer.

Upvotes: 0

Views: 9413

Answers (2)

MohanaRao SV
MohanaRao SV

Reputation: 1125

Below tag iterating over map for displaying it's key and respective values.    
    <s:iterator value="nationalities">
       <s:property value="key" /> <s:property value="value" />
    </s:iterator> 

Upvotes: -1

PrimosK
PrimosK

Reputation: 13918

Your JSP should look something like this:

<s:form action="YourSubmitAction">
    <s:select list="nationalities" name="choosenNationality"/>
    <s:submit/>
</s:form>

If you would like to submit chosen value, you have to create action YourSubmitAction and don't forget to create choosenNationality field of type String with setter - it will be populated with the corresponding key (A/B in your case).

Upvotes: 4

Related Questions