*
The moose likes JSF and the fly likes Using Map with t:selectItems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Using Map with t:selectItems" Watch "Using Map with t:selectItems" New topic
Author

Using Map with t:selectItems

Shasi Mitra
Ranch Hand

Joined: Nov 27, 2008
Posts: 101

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

Joined: Jun 25, 2001
Posts: 15957
    
  19

You cannot attach collections directly to the JSF selection controls. You have to front their elements with SelectItem's. For example:



Customer surveys are for companies who didn't pay proper attention to begin with.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Nov 27, 2008
Posts: 101

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

Joined: Oct 08, 2006
Posts: 2458
In this case, you may find this article useful: http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Map with t:selectItems
 
Similar Threads
The action of my commandButton does not work any more after using SelectOneMenu
Dynamically generate jsf components
Setting bean value problem
Error while working with JSF SelectOneMenu and JSTL forEach Tag
how to get both value and text of selected item in action class