File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes populate selectOneMenu with list containing various type of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "populate selectOneMenu with list containing various type of object" Watch "populate selectOneMenu with list containing various type of object" New topic
Author

populate selectOneMenu with list containing various type of object

Maciej Kawczynski
Greenhorn

Joined: Nov 03, 2011
Posts: 3
Is it possible to populate selectOneMenu with list containing various type of objects ?
For example:
List<Object>
and then during submit check what object it parsing ?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch Maciej

Typically, we select one from *one kind* of items . so why do you want to select one from a list containing various type of objects ?

Please also look at this class http://download.oracle.com/javaee/6/api/javax/faces/model/SelectItem.html
Maciej Kawczynski
Greenhorn

Joined: Nov 03, 2011
Posts: 3
Thank you for your answer.

I have to make dropdown with different Object types, because i have
3 objects with almost all various fields, only id, name, price and type are the same.




i have converter for all objects.

selectOneMenu should look like this:

<h:selectOneMenu value="#{bean.object}" valueChangeListener="#{bean.listener}"
<f:selectItems value="#{bean.objects}" var="o" itemLabel="#{o.name} - #{o.type}" itemValue="#{o}"/>
<a4j:ajax event="change" render="@this"/>
</h:selectOneMenu>

And listener is not firing same like bean.object is not set while change.
Only converter for each object getAsString method is firing.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I think you are complicated the things. why do you want to set an object as select box value/option ? *browser interpret that object as string*! and when a user select air or car and submit the form then create an appropriate object on server, if you need.
in jsp-->
<h:selectOneMenu binding="#{pc_Welcome.travelModeSelectOneMenu}"/>

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and as in your example, f:selectItems doesnt have itemLable and itemValue attribute, and instead it has value attribute which expect a collection/single of SelectItem.
additionally, f:selectItem has all the above three attribute but value attribute will take the precedence .
Maciej Kawczynski
Greenhorn

Joined: Nov 03, 2011
Posts: 3
i resolved my issue, i just create base object TransportOption which has 4 fields like id, name, type, price and objects like Car, Airplane, Bus extends this object. f:selectItems populate List<TransportObtion> and now everything work very well .
Thanks all for your suggestions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: populate selectOneMenu with list containing various type of object