• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

populating drop down from HashMap

 
Sudha Ramasamy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm trying to populate a dropDown using h:selectOneMenu and f:selecItems tag using HashMap<String,String> values

I understand when displaying drop down using f:selectItems tag populates the drop down label using HashMap Key and value using HashMap value,
is there a way to populate dropdown label using hashmap value and drop down value as hashmap value

I came across few solutions like
1.swap the key/value in Map before passing it to f:selectItems
2.construct selectItem list in java and pass the list to UI

but i dont think both of the above two ideas are better,

I'm using EL1.2 version which doesn't support map.entrySet() in populating f:selectItems like
<f:selectItems value="#{bean.map.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}" />

is there any other way to solve this?

Thanks,
Sudha
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Sudha!

Actually, I don't recommend coding logic on your View definition. It couples the Model and View too closely, which makes maintenance more expensive. Besides, why should the View care what form of internal storage the Model is using for an object?

So my preferred method is to use the basis f:selectItems tag with the value= attribute and put all the selectItem list-building logic in the backing bean where it's easier to debug and you maintain a higher level of abstraction as well.

A SelectItem list is a sequential enumeration of labels which have associated values. As such, you would want to provide some sort of ordering of those labels so that everything show up consistently from view to view. Not to mention that doing things like putting the labels in collating sequence makes it easier for users to find them.

A Map, on the other hand, is a random-access container with no inherently-guaranteed order of enumeration. That's why you need the intermediary. To convert the random Map to a linear (sequential) collection and to provide an ordering of your choice to that collection.
 
Sudha Ramasamy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
Thanks for your reply,

BTW
"................ So my preferred method is to use the basis f:selectItems tag with the value= attribute and put all the selectItem list-building logic in the backing bean where it's easier to debug and you maintain a higher level of abstraction as well."

do you mean construct the selectItem list in Model and pass that list to View to display it ?

Thanks,
Sudha
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the correct idea. The more precise terminology would be that you'd construct the SelectItem list (or array) as a sub-model of the backing bean (Model), and expose it as a property. Then code the value= of the f:selectItem tag to reference that property. The SelectOneMenu Controller would then retrieve that property value to assist the JSF HTML renderer in constructing the corresponding HTML <OPTION> elements on the actual returned webpage.

It goes deeper than that, of course, but I was mostly just pointing out the Model/View/Controller aspects of the process.
 
Sudha Ramasamy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got your point Tim,

Thanks for the nice detailed explanation !

Regards,
Sudha

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic