Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Map with t:selectItems

 
Shasi Mitra
Ranch Hand
Posts: 101
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all - I have a map whose key is a string and value a ValueObject. Can anybody suggest how to use it with <t:selectItems>
I tried <t:selectItems value="#{myBean.myMap.entrySet}" var="item" itemValue="#{item.value}" itemLabel="#{item.key}" />. It is not populating the drop down. If i give <t:selectItems value="#{myBean.myMap}" var="item" /> it is throwing null pointer exception. Any clue on how to make it work? My requirement is, similar to how we bind one datatable and get the entire row which the user selects.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot attach collections directly to the JSF selection controls. You have to front their elements with SelectItem's. For example:


 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look, he is using the Tomahawk's variant of f:selectItems, the t:selectItems. It behaves a bit different, you need to specify the var and the itemValue and itemLabel attributes for each iterated item from the 'var'.

But you can also just use f:selectItems for this. It accepts a Map<String, Object> as well where map keys are item labels and map values are item values. Normal way is indeed to use SelectItem[] or List<SelectItem> for this.

 
Shasi Mitra
Ranch Hand
Posts: 101
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i use List of selectitem objects I am getting the following exception

"java.lang.IllegalArgumentException: Value is no String".

Each Selectitem was created as following:

Selectitem sel = new Selectitem(customObj, cusomObj.getDisplayValue());
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, you may find this article useful: http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic