品牌和型号有两个selectOneMenu组件。我希望在品牌选择改变时,用ajax更新模型。现在没有任何反应我的错在哪里?
CarBean.java
@ManagedBean(name = "carbean")
@ViewScoped
public class CarBean implements Serializable {
private Brand brand;
private Model model;
private List<brand> brandList = new ArrayList<brand>();
private List<model> modelList = new ArrayList<model>();
@PostConstruct
public void init() {
System.out.println("PostConstruct");
brandList.add(new Brand(1, "Audi"));
brandList.add(new Brand(2, "Opel"));
modelList.add(new Model(1, "A1"));
modelList.add(new Model(2, "A3"));
}
public void brandChanged(AjaxBehaviorEvent event) {
System.out.println("brandChanged");
modelList.clear();
if (brand.getId() == 1) {
modelList.add(new Model(1, "A1"));
modelList.add(new Model(2, "A3"));
} else if (brand.getId() == 2) {
modelList.add(new Model(3, "Astra"));
modelList.add(new Model(4, "Corsa"));
}
}
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public List<brand> getBrandList() {
return brandList;
}
public void setBrandList(List<brand> brandList) {
this.brandList = brandList;
}
public List<model> getModelList() {
return modelList;
}
public void setModelList(List<model> modelList) {
this.modelList = modelList;
}
}
home.xhtml
<h:form>
<h:selectOneMenu id="brandid" value="#{carbean.brand}">
<f:selectItems value="#{carbean.brandList}" var="brand" itemValue="#{brand.id}" itemLabel="#{brand.name}" />
<f:ajax event="change" listener="#{carbean.brandChanged}" execute="@this" render="modelid" />
</h:selectOneMenu>
<h:selectOneMenu id="modelid" value="#{carbean.model}">
<f:selectItems value="#{carbean.modelList}" var="model" itemValue="#{model.id}" itemLabel="#{model.name}" />
</h:selectOneMenu>
</h:form>