Forums Register Login

populating drop down from HashMap

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

I got your point Tim,

Thanks for the nice detailed explanation !

Regards,
Sudha

It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4221 times.
Similar Threads
display dynamically
Dependent Drop- Down
Problem with dynamic drop down
HashMap, DropDown List, order of values
Struts2 label tag - key attribute is being replaced with actual value?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:52:48.