Today, I developed a simple lesson for chained list. I am having combo1 showing the names of two states (tamilnad and kerala).
The user selects the desired state from the combo and clicks button1. The important cities from that state are displayed in list1.
This is the first step for ajaxified example to be developed. The facelet code is given below.
I always preferred to name the facelet file as view.xhtml.
//view.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<body bgcolor="green" text="cyan">
<h:form>
SELECT THE STATE, YOU WILL GET THE CITIES.<br/>
<h:selectOneMenu id="list1" value="#{cityguide.state}">
<f:selectItem itemLabel="tamilnad" itemValue="tamilnad"/>
<f:selectItem itemLabel="kerala" itemValue="kerala"/>
</h:selectOneMenu>
<h:commandButton id='button1' value="show cities"
action="#{cityguide.show}" /> </h:selectOneListbox>
</h:form>
</body>
</html>
************************************
cityguide.java is the corresponding backing bean. as given below,
//cityguide.java
package mypack;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import javax.faces.event.*;
import java.util.*;
@ManagedBean(name="cityguide")
@SessionScoped
public class cityguide implements java.io.Serializable
{
String state;
String city;
Vector cities;
public void setState(String a){state=a;}
public String getState() {return state;}
public void setCity(String a){city=a;}
public String getCity() {return city;}
public void setCities(Vector v){cities=v;}
public Vector getCities() { return cities;}
//--------------------------------------
public String show()
{
String a = getState();
Vector vector1 = new Vector();
if(a.equals("tamilnad"))
{
vector1.removeAllElements();
vector1.add("Chennai");
vector1.add("trichy");
vector1.add("kovai");
vector1.add("madurai");
}
if(a.equals("kerala"))
{
vector1.removeAllElements();
vector1.add("quilon");
vector1.add("kochin");
vector1.add("trivandrum");
vector1.add(" trissur");
}
System.out.println(""+vector1);
setCities(vector1);
return "";
}
}
Then type the url as below
"http://localhost:8080/jsf2chainedapp/view.jsf"
In the next post I will use AJAX for the same purpose. This example gives dynamic loading of listbox. There is no iteration code. I have simply bound a collection to list1.