PrimeFaces单选按钮上的默认选择

时间:2014-08-18 01:06:08

标签: jsf jsf-2 primefaces

我有一个现场一个单选按钮:

XHTML:

<div class="col-md-4">
    <p:selectOneRadio id="gender"
        value="#{oneGoBean.gender}" required="true"
        requiredMessage="Gender Required">
        <f:selectItems value="#{oneGoBean.gender}" />
    </p:selectOneRadio>
</div>

豆:

import javax.faces.model.SelectItem;

private ArrayList<SelectItem> gender;

public final ArrayList<SelectItem> getGender() {
    return gender;
}

public final void setGender(final ArrayList<SelectItem> gender) {
    this.gender = gender;
}

public final void loadGender() {
    gender = new ArrayList<SelectItem>();
    gender.add(new SelectItem("M", "Male"));
    gender.add(new SelectItem("F", "Female"));
}

在加载期间,两个性别正在填充arraylist。问题是,网页上没有默认值。两个单选按钮均未选中。任何人都知道为什么,我想默认选择一个按钮(男性)?

1 个答案:

答案 0 :(得分:4)

您正在将单选按钮的值映射到值列表,但应将其映射到单个值。类似的东西:

XHTML:

<div class="col-md-4">
    <p:selectOneRadio id="gender"
        value="#{oneGoBean.gender}" required="true"
        requiredMessage="Gender Required">
        <f:selectItems value="#{oneGoBean.genderList}" />
    </p:selectOneRadio>
</div>

豆:

import javax.faces.model.SelectItem;

private ArrayList<SelectItem> genderList;
private String gender = "M"; //give the default value

public final ArrayList<SelectItem> getGenderList() {
    return genderList;
}

public final void setGenderList(final ArrayList<SelectItem> genderList) {
    this.genderList = genderList;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public final void loadGender() {
    genderList = new ArrayList<SelectItem>();
    genderList.add(new SelectItem("M", "Male"));
    genderList.add(new SelectItem("F", "Female"));
}