如何设置默认值<p:selectonemenu

时间:2015-07-30 00:25:11

标签: java jsf primefaces selectonemenu

=“”

我有一个包含两个<p:selectOneMenu ...>组件的jsf。

JSF的一部分:

<p:selectOneMenu id="speakerLanguage"
        value="#{speakerAddFormView.currentSpeaker.nativLang1}">
    <f:selectItem itemLabel="Select Language" itemValue="-1" />
    <f:selectItems value="#{speakerAddFormView.languages.entrySet()}"
            var="languages" itemLabel="#{languages.key}"
            itemValue="#{languages.value}" />
    <p:ajax listener="#{speakerAddFormView.updateStateSecondNativeLangSOM}"
            update="speakerLanguage2" />
</p:selectOneMenu>
<p:selectOneMenu id="speakerLanguage2"
        value="#{speakerAddFormView.currentSpeaker.nativLang2}"
        disabled="#{speakerAddFormView.disableSecondNativeLangSOM}">
    <f:selectItem itemLabel="Select second Language" itemValue="-1" />
    <f:selectItems value="#{speakerAddFormView.languages.entrySet()}"
            var="languages" itemLabel="#{languages.key}"
            itemValue="#{languages.value}"/>
</p:selectOneMenu>

在我的bean中,我有一个包含本地语言的public static final Map<String, String> LANGUAGE_CODES = new TreeMap<String, String>();。在初始化bean时引用此TreeMap

@PostConstruct
public void init() {
    this.languages = ConstantCodes.LANGUAGE_CODES;
}

当我重新加载页面时,两个<p:selectOneMenus >都被选中,而不是默认的<f:selectItem itemLabel="Select Language" itemValue="-1" /><f:selectItem itemLabel="Select second Language" itemValue="-1" />

屏幕剪辑: Wrong selected entries

修改:从头开始选择这两项。 Selected Items

有没有办法强制默认选择的值?

1 个答案:

答案 0 :(得分:1)

nativLang1方法中将nativLang2-1设置为@PostConstruct

@PostConstruct
public void init() {
    System.out.println("initializing");
    this.languages = ConstantCodes.LANGUAGE_CODES;
    nativLang1 = "-1";
    nativLang2 = "-1";
}