我意识到这是一篇旧帖子,但也许我的回答可能对某人有所帮助。这是一个填充第二个下拉列表的工作示例:
这是支持 bean:
package com.test.beans;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIInput;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
@ManagedBean(name = "countryBean")
@RequestScoped
public class CountryBean {
private List<SelectItem> country;
private List<SelectItem> region;
public CountryBean() {
country = new ArrayList<SelectItem>();
region = new ArrayList<SelectItem>();
}
public void changeRegion(AjaxBehaviorEvent ev) {
// clear the drop-down
region.clear();
String value = "";
if (ev == null) {
System.out.println("Ajax event is null!");
} else {
UIInput input = (UIInput) ev.getSource();
value = (String) input.getValue();
}
if (value.equals("Canada")) {
System.out.println("CND value!");
region.add(new SelectItem("Alberta"));
region.add(new SelectItem("British Columbia"));
region.add(new SelectItem("Manitoba"));
region.add(new SelectItem("New Brunswick"));
region.add(new SelectItem("Newfoundland & Labrador"));
region.add(new SelectItem("Northwest Territories"));
region.add(new SelectItem("Nova Scotia"));
region.add(new SelectItem("Nunavut"));
region.add(new SelectItem("Ontario"));
region.add(new SelectItem("Quebec"));
region.add(new SelectItem("Prince Edward Island"));
region.add(new SelectItem("Saskatchewan"));
region.add(new SelectItem("Yukon"));
} else if (value.equals("USA")) {
System.out.println("USA value!");
region.add(new SelectItem("Alabama"));
region.add(new SelectItem("Alaska"));
region.add(new SelectItem("Arizona"));
region.add(new SelectItem("Arkansas"));
region.add(new SelectItem("California"));
region.add(new SelectItem("Colorado"));
region.add(new SelectItem("Connecticut"));
region.add(new SelectItem("Delaware"));
region.add(new SelectItem("Florida"));
region.add(new SelectItem("Georgia"));
region.add(new SelectItem("Hawaii"));
region.add(new SelectItem("Idaho"));
region.add(new SelectItem("Illinois"));
region.add(new SelectItem("Indiana"));
region.add(new SelectItem("Iowa"));
region.add(new SelectItem("Kansas"));
region.add(new SelectItem("Kentucky"));
region.add(new SelectItem("Louisiana"));
region.add(new SelectItem("Maine"));
region.add(new SelectItem("Maryland"));
region.add(new SelectItem("Massachusetts"));
region.add(new SelectItem("Michigan"));
region.add(new SelectItem("Minnesota"));
region.add(new SelectItem("Mississippi"));
region.add(new SelectItem("Missouri"));
region.add(new SelectItem("Montana"));
region.add(new SelectItem("Nebraska"));
region.add(new SelectItem("Nevada"));
region.add(new SelectItem("New Hampshire"));
region.add(new SelectItem("New Jersey"));
region.add(new SelectItem("New Mexico"));
region.add(new SelectItem("New York"));
region.add(new SelectItem("North Carolina"));
region.add(new SelectItem("North Dakota"));
region.add(new SelectItem("Ohio"));
region.add(new SelectItem("Oklahoma"));
region.add(new SelectItem("Oregon"));
region.add(new SelectItem("Pennsylvania"));
region.add(new SelectItem("Rhode Island"));
region.add(new SelectItem("South Carolina"));
region.add(new SelectItem("South Dakota"));
region.add(new SelectItem("Tennessee"));
region.add(new SelectItem("Texas"));
region.add(new SelectItem("Utah"));
region.add(new SelectItem("Vermont"));
region.add(new SelectItem("Virginia"));
region.add(new SelectItem("Washington"));
region.add(new SelectItem("West Virginia"));
region.add(new SelectItem("Wisconsin"));
region.add(new SelectItem("Wyoming"));
}
}
// getters and setters
public List<SelectItem> getCountry() {
return country;
}
public void setCountry(List<SelectItem> country) {
this.country = country;
}
public List<SelectItem> getRegion() {
return region;
}
public void setRegion(List<SelectItem> region) {
this.region = region;
}
}
这是形式:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Form</title>
</h:head>
<h:body>
<h:panelGrid columns="2">
<h:form>
<!-- country -->
<h:outputText value="Select a country: "/>
<h:selectOneMenu id="countryDropDown">
<f:selectItem itemLabel="Canada" itemValue="Canada" />
<f:selectItem itemLabel="USA" itemValue="USA" />
<f:ajax listener="#{countryBean.changeRegion}" render="regionDropDown" />
</h:selectOneMenu>
<!-- region -->
<h:outputText value="Select a region: "/>
<h:selectOneMenu id="regionDropDown">
<f:selectItems value="#{countryBean.region}" />
</h:selectOneMenu>
</h:form>
</h:panelGrid>
</h:body>
</html>