Primefaces SelectOneRadio with an noSelectionOption selected

How can I have a selectOneRadio with an noSelectionOption which is selected by default?

I have the following:

<p:selectOneRadio>
    <f:selectItem itemLabel="none" noSelectionOption="true"/>
    <f:selectItems value="#{bean.anything}"/>
</p:selectOneRadio>

I want to have the "none" selected by default? How do I do that? As there is no "selected" Attribute for

Upvotes: 3

Views: 4581

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Use a managed bean field bound to your view with null value, also have your unselected option with null value.

JSF part:

<p:selectOneRadio value="#{bean.foo}">
    <f:selectItem itemLabel="none" itemValue="#{null}" noSelectionOption="true"/>
    <f:selectItems value="#{bean.anything}"/>
</p:selectOneRadio>

Managed bean code:

@ManagedBean
@RequestScoped
public class Bean {
    //its value by default will be null
    private String foo;
    //getters and setters...
}

Upvotes: 2

Related Questions