Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Maps - Struts 1

 
pavan mane
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move to Struts2 and you will get Solution for your Struts problem.

It has Struts1.x plug-in too.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting, thanks for the explanation.
 
John Woo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic