我有2个下拉列表:Country&州。我想在更改国家/地区列表的选择时重新加载状态列表。 我试过了How to pass value into Listener in selectOneMenu。 但是,我的代码中的更改事件不起作用。任何人都可以帮助我吗?谢谢。
<h:selectOneMenu id="ddlCountry" value="#{Bean.companyCountry}" >
<f:selectItems value="#{Bean.countries}" var="country" itemValue="#{country.value}" itemLabel="#{country.name}" />
<f:ajax event="change" listener="#{Bean.loadStates}" render="ddlState" execute="@this"/>
</h:selectOneMenu>
<h:selectOneMenu id="ddlState" value="#{Bean.companyState}">
<f:selectItems value="#{Bean.states}" var="state" itemValue="#{state.value}" itemLabel="#{state.name}" />
</h:selectOneMenu>
Java Bean类sscce
private ArrayList<State> states;
public ArrayList<State> getStates() {
return states;
}
public void loadStates(AjaxBehaviorEvent event) {
states = new ArrayList<State>();
if(companyCountry.equals("USA")){
states.add(new State("1","UT"));
states.add(new State("2","WA"));
states.add(new State("3","CA"));
}
}
jsf ajax监听器仍无法在我的项目中工作。所以我使用了primefaces标签,它可以解决我的问题。
<h:selectOneMenu id="ddlCountry" value="#{Bean.companyCountry}" >
<f:selectItems value="#{Bean.countries}" var="country" itemValue="#{country.value}" itemLabel="#{country.name}" />
<p:ajax event="valueChange" update="ddlState" process="@this"/>
</h:selectOneMenu>
<h:selectOneMenu id="ddlState" value="#{Bean.companyState}">
<f:selectItems value="#{Bean.states}" var="state" itemValue="#{state.value}" itemLabel="#{state.name}" />
</h:selectOneMenu>
Java Bean类sscce
private ArrayList<State> states;
public ArrayList<State> getStates() {
states = new ArrayList<State>();
if(companyCountry.equals("USA")){
states.add(new State("1","UT"));
states.add(new State("2","WA"));
states.add(new State("3","CA"));
}
else{
states.clear();
}
return states;
}
答案 0 :(得分:0)
将您的bean范围从@RequestScoped
更改为@ViewScoped
否则,在您调用listener
方法之后,将创建一个新的bean实例,而另一个h:selectOneMenu
将填充空值