0

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;
}

}

learner
  • 1
  • 2
  • 1
    Start by reading [ask] then [mcve] and http://www.stackoverflow.com/tags/jsf/info and improve your question after reading all this. – Kukeltje Oct 28 '15 at 14:25
  • See similar [example](http://stackoverflow.com/questions/22274026/show-hide-panel-when-checkbox-is-selected/22277597#22277597) – Vasil Lukach Oct 28 '15 at 16:05
  • 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 Nov 02 '15 at 13:49
  • newfile.xhtm – learner Nov 02 '15 at 13:54
  • I have updated the ticket.Please check it and assist me in case of any modifications. – learner Nov 02 '15 at 14:06
  • Where are you ensuring that `rendered="#{not empty selectsBean.currentType}">` would evaluate `true`? You're not doing that anywhere in the code. Don't you rather mean to check here if the list of available items is not empty? Nonetheless, that `valueChangeListener` approach is JSF 1.x-ish. See also a.o. http://stackoverflow.com/questions/11505248/how-to-display-dependent-hselectonemenus-with-a-list-of-countries-and-cities/ for a kickoff example of the right approach. All you need to do is to substitute `` with `` (although it has no additional benefits in this specific case) – BalusC Nov 02 '15 at 14:44
  • @BalusC i tried changing the with .And also replaced valueChangeListener with listener in and also implemented the function in bean.But no luck it still the same :(. Also could you please tell me how can we ensure that rendered="#{not empty selectsBean.currentType}"> evaluates to true. Please assist. – learner Nov 03 '15 at 14:10
  • Link tells to put bean in view scope. – BalusC Nov 03 '15 at 18:38
  • I have changed the xhtml file as below and the listener function as below: – learner Nov 04 '15 at 11:25
  • public void getSampleFun(AjaxBehaviorEvent event){ System.out.println("Listener is called"); secondList.clear(); if (null != getCurrentType()) { String[] currentItems; if (((String) getCurrentType()).equals("fruits")) { currentItems = FRUITS; } else { currentItems = VEGETABLES; } for (int i = 0; i < currentItems.length; i++) { SelectItem item = new SelectItem(currentItems[i]); secondList.add(item); } } } – learner Nov 04 '15 at 11:27
  • But still the value change event is not fired :(. I have changed the bean to view scope as well with no luck. – learner Nov 04 '15 at 11:30
  • I am using following jars: JSF: jsf-api-2.0,jsf-facelets,jsf-impl-2.0.2 Rich faces:richfaces-core-api-4.0.0.Final,richfaces-core-impl-4.0.0.Final,richfaces-components-api-4.0.0.Final,richfaces-components-ui-4.0.0.Final,guava-r09,cssparser-0.9.5.jar,sac-1.3-src.jar – learner Nov 04 '15 at 13:11
  • I am using Eclipse Luna IDE and Tomcatv7.0 web server – learner Nov 04 '15 at 14:57

1 Answers1

0

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

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • Thanks for the reply. I tried changing it but no luck. So i replaced with . You can check my comments for the same. But no luck :(. – learner Nov 10 '15 at 09:55