Default selection for <f:selectItem> within <h:selectOneMenu>

76,558

Solution 1

<h:selectOneMenu id="items" value="#{bean.selectedItem}">
  <f:selectItem itemLabel="10" itemValue="10"/>
  <f:selectItem itemLabel="20" itemValue="20"/>
  <f:selectItem itemLabel="30" itemValue="30"/>
</h:selectOneMenu>

The default selection would be the one which has value same as selectedItem which you set in bean.

selectedItem = 20;

Solution 2

Initialize the recordsPerPage in your backing bean.

From your source code I assume that you have a bean FileSearchCriteriaOut and your recordsPerPage is a String, then you can do the following in the bean's constructor:

public FileSearchCriteriaOut() {
   recordsPerPage = "20";
}

For the facelet refer to Jigar Joshi's answer.

Share:
76,558
MTPy
Author by

MTPy

JEE,JSP,JSR-168 Portlets,Spring Batch,JPA

Updated on September 10, 2020

Comments

  • MTPy
    MTPy almost 4 years

    How to make default selection for <f:selectItem> within <h:selectOneMenu>?

    It's needed,that particular "20" item of dropdown to be already selected when page is loaded.

      <h:selectOneMenu value="#{fileSearchCriteriaOut.recordsPerPage}"  >            
                   <f:selectItem itemLabel="5" itemValue="5" />
                   <f:selectItem itemLabel="10" itemValue="10" />
                   <f:selectItem itemLabel="20" itemValue="20" selected="true"/>
      </h:selectOneMenu>
    

    these four don't work:

    <f:selectItem itemLabel="20" selected="true"/>
    <f:selectItem itemLabel="20" selected="selected"/>
    <f:selectItem itemLabel="20" checked="checked"/>
    <f:selectItem itemLabel="20" checked="true"/>