0

There are two selectOneMenu components for brand and model. I want that when brand selection changed, update model with ajax. Now nothing happens. Where is my fault?

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>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Ali Kurnaz
  • 57
  • 8
  • If the class really is called CarBean then I'd expect the name in your facelets page to be `carBean`, not `carbean`. Case is important. – Gimby Mar 17 '16 at 08:23
  • 1
    Run your application in development mode. Lots more info then about components not being found etc. – Kukeltje Mar 17 '16 at 08:24
  • Indeed, undisplayed faces messages such as "Validation Error: Value is not valid" will also be logged in development mode. When you simply copypaste the error message into a search engine, you'll get answers. – BalusC Mar 17 '16 at 08:37

0 Answers0