0
votes

I am trying to populate a drop down box based on the selection of radio button option.I am using rich faces but not able to do so. I also have other inputs in the same page which will be stored in the backing bean.I am using jsf 1.2. Can anyone please assist.

i switched to jsf 2.0 and rich faces 4

here is my code but it never works

new file.xhtml

<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:form>
    <h:selectOneMenu value="#{selectsBean.currentType}" valueChangeListener="#{selectsBean.valueChanged}">
        <f:selectItems value="#{selectsBean.firstList}" />
        <a4j:ajax event="valueChange" render="second" execute="@this" />
    </h:selectOneMenu>
    <a4j:outputPanel id="second" layout="block">
        <h:selectOneMenu value="#{selectsBean.currentType}" rendered="#{not empty selectsBean.currentType}">
            <f:selectItems value="#{selectsBean.secondList}" />
        </h:selectOneMenu>
    </a4j:outputPanel>
</h:form>
</ui:composition>




package test.com;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;


@ManagedBean(name = "selectsBean")
@RequestScoped
public class SelectsBean {
private static final String[] FRUITS = { "", "Banana", "Cranberry", "Blueberry", "Orange" };
private static final String[] VEGETABLES = { "", "Potatoes", "Broccoli", "Garlic", "Carrot" };
private String currentItem = "";
private String currentType = "";
private List<SelectItem> firstList = new ArrayList<SelectItem>();
private List<SelectItem> secondList = new ArrayList<SelectItem>();

public SelectsBean() {
    SelectItem item = new SelectItem("", "");

    firstList.add(item);
    item = new SelectItem("fruits", "Fruits");
    firstList.add(item);
    item = new SelectItem("vegetables", "Vegetables");
    firstList.add(item);

    for (int i = 0; i < FRUITS.length; i++) {
        item = new SelectItem(FRUITS[i]);
    }
}

public List<SelectItem> getFirstList() {
    return firstList;
}

public List<SelectItem> getSecondList() {
    return secondList;
}

public static String[] getFRUITS() {
    return FRUITS;
}

public static String[] getVEGETABLES() {
    return VEGETABLES;
}

public void valueChanged(ValueChangeEvent event) {
    secondList.clear();
    if (null != event.getNewValue()) {
        String[] currentItems;

        if (((String) event.getNewValue()).equals("fruits")) {
            currentItems = FRUITS;
        } else {
            currentItems = VEGETABLES;
        }

        for (int i = 0; i < currentItems.length; i++) {
            SelectItem item = new SelectItem(currentItems[i]);

            secondList.add(item);
        }
    }
}

public String getCurrentType() {
    return currentType;
}

public void setCurrentType(String currentType) {
    this.currentType = currentType;
}

public String getCurrentItem() {
    return currentItem;
}

public void setCurrentItem(String currentItem) {
    this.currentItem = currentItem;
}

}

1
Start by reading How to Ask then minimal reproducible example and stackoverflow.com/tags/jsf/info and improve your question after reading all this.Kukeltje
See similar exampleVasil Lukach
Here is the code which i am trying to run using jsf 2.0 and rich faces 4.0 but it never renders the second menu.learner
newfile.xhtm <h:form><h:selectOneMenu value="#{selectsBean.currentType}" valueChangeListener="#{selectsBean.valueChanged}"> <f:selectItems value="#{selectsBean.firstList}" /> <a4j:ajax event="valueChange" render="second"execute="@this" /> </h:selectOneMenu> <a4j:outputPanel id="second" layout="block"> <h:selectOneMenu value="#{selectsBean.currentType}" rendered="#{not empty selectsBean.currentType}"> <f:selectItems value="#{selectsBean.secondList}" /> </h:selectOneMenu> </a4j:outputPanel> </h:form>learner
I have updated the ticket.Please check it and assist me in case of any modifications.learner

1 Answers

0
votes

You have used wrong event. Change ajax event to change and try again: <a4j:ajax event="change" render="second" />.