Primefaces SelectOneRadio带有一个noSelectionOption选项被选中

3

如何在默认情况下选择一个没有选择选项的selectOneRadio?

我有以下内容:

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

我希望默认情况下选择"none"。我该怎么做?因为没有 "selected" 属性适用于。
1个回答

2

使用一个与视图绑定的受管理bean字段,并将其值设置为null,同时让未选择的选项也具有null值。

JSF部分:

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

托管Bean代码:

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

4
我的后端Bean中设置了值"null",但如果它返回"null",则selectItem不会被选中。为什么? - Jannis Ötjengerdes
我也是,同样的问题。 - Carlos UCR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接