This article is for beginners who want to try out their first J2EE application. It explains the step by step process to create a web application using JSF 2.0, Rich Faces 3.3 for UI and MYSQL5.5 for database and Jboss 5.0.1GA server for deployment.
Set up MYSQL5.5
Set up JBOSS 5.0.1GA
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/company</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>johnsmith</user-name>
<password>password-1</password>
<min-pool-size>2</min-pool-size>
<max-pool-size>10</max-pool-size>
</local-tx-datasource>
</datasources>
Set up Web Application:
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-digester.jar
commons-logging.jar
jboss-faces.jar
jsf-api.jar
jsf-impl.jar
richfaces-api-3.3.3.Final.jar
richfaces-impl-3.3.3.Final.jar
richfaces-ui-3.3.3.Final.jar
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</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>
package com.example.richfaces;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class EmployeeBean {
private String employeeFirstName;
private String employeeLastName;
public String getEmployeeFirstName() {
return employeeFirstName;
}
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
public String getEmployeeLastName() {
return employeeLastName;
}
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
public void doGetEmployeeName() {
InitialContext ctx = null;
DataSource ds = null;
Connection conn = null;
Statement stmt = null;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:/MySqlDS");
conn = ds.getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select employeeFirstName, employeeLastName from EMPLOYEE");
while (rs.next()) {
setEmployeeFirstName(rs.getString(1));
setEmployeeLastName(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
// Implement finally block and better Exception handling here
ex.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<description>Employee Bean</description>
<managed-bean-name>employee</managed-bean-name>
<managed-bean-class>com.example.richfaces.EmployeeBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>employeeFirstName</property-name>
<property-class>java.lang.String</property-class>
<value />
</managed-property>
<managed-property>
<property-name>employeeLastName</property-name>
<property-class>java.lang.String</property-class>
<value />
</managed-property>
</managed-bean>
</faces-config>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!-- RichFaces tag library declaration -->
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<html>
<head>
<title>RichFaces Example</title>
</head>
<body>
<f:view>
<a4j:form>
<rich:panel header="RichFaces Greeter" style="width: 315px">
<a4j:commandButton value="Get Our Employee" reRender="greeting"
action="#{employee.doGetEmployeeName}" />
<h:panelGroup id="greeting">
<h:outputText value="Hello, "
rendered="#{not empty employee.employeeFirstName}" />
<h:outputText value="#{employee.employeeFirstName}" />
<h:outputText value="#{employee.employeeLastName}"
rendered="#{not empty employee.employeeFirstName}" />
</h:panelGroup>
</rich:panel>
</a4j:form>
</f:view>
</body>
</html>
No comments:
Post a Comment