I have a select menu like this :
<h:selectOneMenu value="#{serverMB.selectedServerType}" >
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}" render="selectservertype"/>
</h:selectOneMenu>
I want the default value of the select menu to be the value of one object that has been filled before, something like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}" default="serverMB.server.servertype"> // <- default
or like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem value"serverMB.server.servertype"/> <- but this item was the first one and has been selected
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
</h:selectOneMenu>
You can do something like this if your object is already populated when you call the page, if it is not, it will show the <f:selectItem/>
message:
<h:selectOneMenu value="#{serverMB.server.servertype}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem itemLabel="Select a Server..." />
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}"/>
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}"
render="selectservertype"/>
</h:selectOneMenu>
You can also use the OmniFaces converter.
Here is my example. It works fine for me. (Note: Year is a object. Make sure, you have equals implemented. Year is name
and id
only.)
<h:selectOneMenu value="#{bean.user.year}"
converter="omnifaces.SelectItemsConverter">
<f:selectItem itemLabel="Please select a year." itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{bean.allyears}" var="year"
itemValue="#{year}" itemLabel="#{year.name}" />
</h:selectOneMenu>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With