How To Set Default Selection for f:selectItem within h:selectOneMenu

Sometimes we want to set default value for our combo box in the page. On HTML we only need to add attribute “selected” on the options of <select> element that we choose to be the default. But will be different on JSF since we can not add “selected” attribute to the option that generated by the bean objects. Here is my colleague do for this case and might be useful for you and me in the future:

<h:selectOneMenu id="myFormOptions" value="#{myBean.dudu}">
	<!-- make the default using selectItem tag -->                             
	<f:selectItem itemLabel="Default Value" itemValue="theDefaultVal" />
	<!-- this are the other options -->
	<f:selectItems value="#{myBean.lulu}" />
</h:selectOneMenu>

Put the default value into selectItem tag and the other options on selectItems tag just like above code. Hope this can be help 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s