I want to add a list box to display the country name from the lists on all countries.When I select for e.g India then in the second list box it will display the states related to India only and the flow continues for state and areas related to state.Please provide the code with and without using database.
hi friend,
Here is a sample code for value changing of list box in JSF, hope this will be helpful for you.
FruitBean.java
package devmanuals;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name="fruit")
@RequestScoped
public class FruitBean {
public int fruitPrice;
private static Map<String,Integer> fruitName;
static{
fruitName = new LinkedHashMap<String,Integer>();
fruitName.put("Select-Fruit", 0);
fruitName.put("Apple", 110);
fruitName.put("Mango", 80);
fruitName.put("Guava", 40);
fruitName.put("Grapes", 90);
fruitName.put("PineApple", 50);
}
public Map<String,Integer> getFruitName() {
return fruitName;
}
public int getFruitPrice() {
return fruitPrice;
}
public void setFruitPrice(int fruitPrice) {
this.fruitPrice = fruitPrice;
}
}
Continue...
FruitPriceListener.java
package devmanuals;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
public class FruitPriceListener implements ValueChangeListener {
@Override
public void processValueChange(ValueChangeEvent e)
throws AbortProcessingException {
FruitBean fruit = (FruitBean) FacesContext.getCurrentInstance().
getExternalContext().getRequestMap().get("fruit");
fruit.setFruitPrice((Integer) e.getNewValue());
System.out.println("Value of component has been changed");
System.out.print("Component which value is being changed :");
System.out.println(e.getComponent());
System.out.println("JSF PhaseId : "+e.getPhaseId());
}
}
Continue...
fruitPrice.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" >
<head>
<title>JSF 2 f valueChangeListener example</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Price of Fruit :"/>
<h:inputText value="#{fruit.fruitPrice}" />
<h:outputText value="Select Fruit :"/>
<h:selectOneListbox value="#{fruit.fruitPrice}" onchange="submit()">
<f:valueChangeListener type="devmanuals.FruitPriceListener" />
<f:selectItems value="#{fruit.fruitName}" />
</h:selectOneListbox>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
Thanks.