aspose file tools*
The moose likes Struts and the fly likes Using Maps - Struts 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Maps - Struts 1" Watch "Using Maps - Struts 1" New topic
Author

Using Maps - Struts 1

pavan mane
Greenhorn

Joined: Oct 23, 2006
Posts: 19
hi,
I have just started using Struts, my problem is using Maps present in a form bean to and fro, from the jsp using <html:select> tag

Ex: -
ActionForm
----------
class MyForm extends ActionForm {
private Map<String, String> selectFields;

public void setSelectFields(Map<String, String> selectFields){
this.selectFields = selectFields;
}

public Map<String, String> getSelectFields() {
return this.selectFields;
}

}

I am a beginner in the world of struts and am unable to releate to available resources on the net. An example of the usage would really help. Thanks.


- Pavan Mane<br /> Certifications -<br /> * SCJP5.0(Tamed The Tiger Grr..)
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I'm familiar with struts 1.1 and 1.2 but not 1.3. To my knowlege struts 1 doesn't have a tag that will generate options for a select input from a Map (I'm assuming thats what you're trying to do). There are tags that will generate list of option from collections (and I'm pretty sure Map doesn't implement collection), so you could try making a getter method in your form that retieves the appropriate collection from the Map.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Move to Struts2 and you will get Solution for your Struts problem.

It has Struts1.x plug-in too.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Struts 1 is perfectly capable of generating a list of options from a Map. Just use the htmlptionsCollection tag. When using a Map use the properties "key" and "value" to represent they key and value of the map. Example:


Merrill
Consultant, Sima Solutions
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Interesting, so does this mean that when struts documentation refers to a tag needing a collection its refering to any thing in the Java Collection Framework, rather than anything thing that just implements java.util.Collection?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Map is a special case. When Struts iterates over a Map, it first calls the entrySet method. Even though Map itself does not implement Collection, the entrySet method of Map returns a Set of Map.Entry objects, and Set does implement Collection.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Very interesting, thanks for the explanation.
John Woo
Greenhorn

Joined: Jul 29, 2008
Posts: 8
As Merrill Higginson suggested, I tested the following and got success.

<html:select property="mySelect" >
<html ptionsCollection property="selectFields" value="key" label="value" />
</html:select>

So you can use MAP in <html:select>, but you can only use the above approach. If you use collections, you have more alternative approaches.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Maps - Struts 1