• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Using Maps - Struts 1

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Move to Struts2 and you will get Solution for your Struts problem.

It has Struts1.x plug-in too.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts 1 is perfectly capable of generating a list of options from a Map. Just use the html:optionsCollection 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting, thanks for the explanation.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic