重新加载时将SelectOneMenu值设置为null

时间:2018-09-04 05:30:22

标签: jsf selectonemenu

我通过一个include标记将SelectOneMenu控件添加到了一个表单中,该控件通过ajax更新了bean属性,问题是当刷新页面时,由于某种原因该属性设置为null,我怀疑这有事情要做生命周期,但我不确定如何解决这个问题。

repo的简化版本复制了行为

主页

<html xmlns...>
    <h:body>
        <h1>Select one</h1>
        <h:form>
            <ui:include src="#{country.page}" />
        </h:form>
    </h:body>
</html>

第一页:只是一个链接,该链接导航到添加控件并设置默认值的页面

<div xmlns=...>
    <h:commandLink action="#{country.goTo2}">go to 2</h:commandLink>
</div>

第二页:在此页面刷新时,该属性设置为null

<div xmlns= ...>
    <h:panelGrid columns="2">           
        Select a country:  
        <h:selectOneMenu value="#{country.localeCode}" valueChangeListener="#{country.countryLocaleCodeChanged}">
            <f:selectItem itemLabel="it is null" itemValue="#{null}" />
            <f:selectItems value="#{country.countryInMap}" />
            <f:ajax execute="@form"/>
        </h:selectOneMenu>
        <h:commandLink action="#{country.goTo1}">go back to 1</h:commandLink>
    </h:panelGrid>
</div>

这是支持豆

@ManagedBean(name="country")
@SessionScoped
public class CountryBean implements Serializable{

    private static Map<String,String> countries;

    private String localeCode = "en"; //default value 

    private String page = "page1.xhtml";

    static{
        countries = new LinkedHashMap<String,String>();
        countries.put("United Kingdom", "en"); //label, value
        countries.put("French", "fr");
        countries.put("German", "de");
        countries.put("China", "zh_CN");
    }

    public void countryLocaleCodeChanged(ValueChangeEvent e){
        System.out.println("value changed :" + e.getNewValue());
    }

    public Map<String,String> getCountryInMap() {
        return CountryBean.countries;
    }

    //setters and getters here

    public void goTo2() {
        this.localeCode = "de";
        this.page = "page2.xhtml";
    }

    public void goTo1() {
        this.page = "page1.xhtml";
    }
}

0 个答案:

没有答案