I am using following specs:
JSF 2.0, Netbeans 7.1.2 with JDK 1.6 and Glassfish 3.1.2
Below is my index.xhtml code:
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Select a country:" />
<h:selectOneMenu id="countryBox" value="#{ab.selCountry}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="United States of America" itemValue="USA" />
<f:ajax execute="countryBox" render="stateBox" event="valueChange" listener="#{ab.result}"/>
</h:selectOneMenu>
<h:outputText value="Select a state:" />
<h:selectOneMenu value="#{ab.country}" id="stateBox">
<f:selectItems value="#{ab.state}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
and here is my AJAXBean.java code:
@ManagedBean (name="ab")
@RequestScoped
public class AJAXBean {
/**
* Creates a new instance of AJAXBean
*/
private List<SelectItem> country;
private List<SelectItem> state;
private String selCountry;
// getters and setters of properties
public AJAXBean()
{
country = new ArrayList<SelectItem>();
state = new ArrayList<SelectItem>();
}
public void result(ValueChangeEvent vce) // valueChangeListener for ajax call. Is this valid?
{
System.out.println("Inside valueChangeListener");
String value = (String) vce.getNewValue();
state.clear();
if (value.equals("India"))
{
state.add(new SelectItem("Maharashtra"));
state.add(new SelectItem("Madhya Pradesh"));
state.add(new SelectItem("Andhra Pradesh"));
state.add(new SelectItem("Uttar Pradesh"));
state.add(new SelectItem("Rajasthan"));
}
else if (value.equals("USA"))
{
state.add(new SelectItem("Missouri"));
state.add(new SelectItem("Kentucky"));
state.add(new SelectItem("North Carolina"));
state.add(new SelectItem("South Carolina"));
state.add(new SelectItem("Colorado"));
}
}
}
Basically what I'm trying to do here is:
One fires an AJAX call on event="valueChange" and listener="#{ab.result}". The result method here modifies the value of 'state' list (of SelectItems).
Is the syntax of my value change listener method correct? Because when I try to execute this, it shows me an alert box stating the MethodNotFoundException + the method #{ab.result(AJAXBehaviorEvent)} not found i.e. it is trying to search for a "result" method which catches an AJAXBehaviorEvent object. So , my question is, Is the signature for a value change listener method any different when it is used with f:ajax call?
PS: It tried to use #{ab.result()}. In this case, it simply says MethodNotFoundException. and If is use #{ab.result(vce)} instead of #{ab.result()} / #{ab.result}, it gives me a NullPointerException.
If I put the same function in valueChangeListener attribute of my h:selectOneMenu, it works,. I mean, obviously it will work. But that does not solve my purpose. My purpose is a simple ajax call on valueChange event which updates a list in the bean and re-renders (a second) linked h:selectOneMenu as a result
What is wrong with my code?