NetBeans creates a new web application with JSF 1.2 and JSTL libraries included. It also provides required JSF entries in web.xml (configuration of FacesServlet) and faces-config.xml
.
The following is the complete
web.xml after filter configuration is added.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>jobs.jsp</welcome-file>
</welcome-file-list>
</web-app>
tag to make an AJAX call to get names of employees who belong to the job selected by the user. The code for JSP is given below.
jobs.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<f:view>
<h:form>
<table>
<tr>
<td>Select Job :
<td>
<h:selectOneMenu value="#{PayrollBean.jobid}">
<f:selectItems value="#{PayrollBean.jobs}"/>
<a4j:support event="onchange" reRender="employeesList"/>
</h:selectOneMenu>
</td>
</tr>
<tr>
<td>Select Employee : </td>
<td>
<h:selectOneMenu value="#{PayrollBean.empid}" id="employeesList" >
<f:selectItems value="#{PayrollBean.employees}"/>
</h:selectOneMenu><br/>
</td>
</tr>
</table>
</h:form>
</f:view>
The first SelectOneMenu takes items from getJobs() method of PayrollBean. Method getJobs() returns a collection of SelectItem object here jobid is the value and jobtitle is text.
It copies the selected jobid into property jobid of PayrollBean.
The most important one in this component is the child tag <a4j:support event="onchange" reRender="employeesList"/>,
which is making an AJAX call to server whenever a different option is selected to populate dropdownlist with id employeesList. This is how we provide support for AJAX in the context of JSF. Tag support is taken from a4j library that is provided by RichFaces.
JSP uses JSF managed bean called PayrollBean, which is given below.
PayrollBean.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
public class PayrollBean {
private String jobid;
private String empid;
public String getEmpid() {
return empid;
}
public void setEmpid(String empid) {
this.empid = empid;
}
public String getJobid() {
return jobid;
}
public void setJobid(String jobid) {
this.jobid = jobid;
System.out.println(jobid);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
private double salary;
public List<SelectItem> getJobs(){
ArrayList<SelectItem> jobs = new ArrayList<SelectItem>();
jobs.add(new SelectItem("0","--- Select Job ---"));
try {
Connection con = getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select job_id,job_title from jobs order by job_title");
while (rs.next()) {
jobs.add( new SelectItem(rs.getString("job_id"),rs.getString("job_title")));
}
rs.close();
st.close();
con.close();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
return jobs;
}
public List<SelectItem> getEmployees(){
ArrayList<SelectItem> employees = new ArrayList<SelectItem>();
employees.add( new SelectItem("0","--Select Employee ---"));
if ( jobid == null || jobid.equals("0"))
return employees;
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("select employee_id, first_name from employees where job_id = ?");
ps.setString(1,jobid);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
employees.add(new SelectItem(rs.getString("employee_id"),rs.getString("first_name")));
}
rs.close();
ps.close();
con.close();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
return employees;
}
public Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
return con;
}
}
The entry related to the above Managed Bean in Faces-config is as follows:
faces-config.xml
<managed-bean>
<managed-bean-name>PayrollBean</managed-bean-name>
<managed-bean-class>PayrollBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Build the project and deploy to Apache tomcat. Running jobs.jsp displays the list of jobs. Select a job to get the list
of employees.