JSF页面:
<h:selectOneMenu id="country" value="#{countryCity.country}">
<f:selectItems value="#{countryCity.countryName}"/>
<a4j:support event="onchange" reRender="city" action="#{countryCity.change}"/>
</h:selectOneMenu>
<h:selectOneMenu id="city">
<f:selectItems value="#{countryCity.currentCityName}"/>
</h:selectOneMenu>
backingbean:
public class CountryCity {
private List<SelectItem> countryName;
private List<List<SelectItem>> cityName;
private List<SelectItem> currentCityName;
private String country;
public CountryCity(){
countryName = new ArrayList();
countryName.add(new SelectItem("0","China"));
countryName.add(new SelectItem("1","England"));
countryName.add(new SelectItem("2","America"));
List<SelectItem> china = new ArrayList();
china.add(new SelectItem("guangzhou","guangzhou"));
china.add(new SelectItem("xianggang","xianggang"));
List<SelectItem> england = new ArrayList();
england.add(new SelectItem("Londun","Londun"));
List<SelectItem> america = new ArrayList();
america.add(new SelectItem("housdun","housdun"));
america.add(new SelectItem("Washdom","Washdom"));
cityName = new ArrayList();
cityName.add(china);
cityName.add(england);
cityName.add(america);
currentCityName = cityName.get(0);
}
public List<SelectItem> getCountryName() {
return countryName;
}
public void setCountryName(List<SelectItem> countryName) {
this.countryName = countryName;
}
public List<List<SelectItem>> getCityName() {
return cityName;
}
public void setCityName(List<List<SelectItem>> cityName) {
this.cityName = cityName;
}
public List<SelectItem> getCurrentCityName() {
return currentCityName;
}
public void setCurrentCityName(List<SelectItem> currentCityName) {
this.currentCityName = currentCityName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void change(){
currentCityName = cityName.get(Integer.parseInt(getCountry()));
}
}
faces-config.xml声明:
<managed-bean>
<managed-bean-name>countryCity</managed-bean-name>
<managed-bean-class>bb.CountryCity</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
范围应该设成session,不然会出现bug。
JSF实现联动菜单(同步):
http://hi.baidu.com/coolcat%5Fpolice/blog/item/77b6013dcf4909c29e3d622f.html