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 select and populates dropdonwlist 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(){
//System.out.println("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.
Home
Blogs
Post Your Comment
Comments
|
|
Posted By madhusudhan On 30-Apr-09 11:01:59 AM
hai sir,
this is madhusudhan im one of our student of JEE(web)in recent passout batch
i have to do a project on struts implementing ajax using DWR,Actually im more interested on jsf rich faces sir , i dont have any idea of implementing ajax on struts.
|
|
|
Posted By srinivasu namburui On 05-Jun-09 10:53:16 AM
Dear madhusudhan,
Please find this below URL for sample Login Application (Struts 2 AJAX).
http://www.roseindia.net/struts/struts2/struts2ajax/index.shtml
I hope this URL will be useful for you for starting up the things for youre project work.
Thanks and Regards
Srinivasu
|
|
|
Posted By madhu On 06-Jun-09 12:12:24 AM
hai sir could u please tell me how to imlement the DWR AJAX in STRUTS without using the DAO pattern
|
|
|
Posted By sanae On 12-Jun-09 09:38:48 PM
i have problem with IE 6.0 plz help me
|
|
|
Posted By Vikram On 13-Jul-09 12:36:49 PM
Nice snippet indeed...:)
@ sanae
Chk whether ur browser is script enabled or not.
# Open Internet Explorer.
# On the Tools menu, click Internet Options.
# On the Security tab, click Internet.
# Click Custom Level.
# Scroll down to Active scripting.
# Click Enable (or Prompt).
# Click OK.
# Click Yes.
# Click OK
PS: IE actually sucks..use firefox for best use..coz opensource that matters now a dayz.
|
|
|
Posted By trinadh On 27-Apr-10 02:48:52 PM
sir pls send me the code which working . the above code i implemented it is not working. pls send me with libraries also. it is very urgent.pls kindly consider my request and send to me mail.
thank you sir
|
|
|
Posted By Hoda Khalid On 20-May-10 10:28:40 AM
I tried this, no compilation error and nothing happens on change too!!
I don't know what to do
|
|
|
Posted By Ajay On 30-Aug-10 05:53:10 PM
It Working fine with my side (Iam using MYSQL) if any one need working code can mail me .
|