Search code examples
jsfseam

Select default value in h:selectOneMenu


I am trying to show a select one menu, such that the value selected by default is the name of the city (if there is a city in the variable "item"). If there is no value, I want to show the "nothing selected".

I was trying to do something like:

<h:selectOneMenu value="#{(item.city!=null)?itemTmp.city.name:''}">
    <s:selectItems .... />
</h:selectOneMenu>

But a Parse Exception is thrown. How can I set a default value in the h:selectOneMenu if there is one, or "no selection" if opposite?


Solution

  • If you want to display the empty value, you must add a select item with an empty string or any other like "--Select City--" to the list.

        cityItemList.add(new SelectItem(""));  
    // or cityItemList.add(new SelectItem("--Select City--"));
        cityItemList.add(new SelectItem("Greenville"));
        cityItemList.add(new SelectItem("Franklin"));
        cityItemList.add(new SelectItem("Clinton"));
        cityItemList.add(new SelectItem("Fairview"));