A Tutorial For Spring Hibernate JSF Richfaces
A Tutorial For Spring Hibernate JSF Richfaces
MOTIVATION
In this basic tutorial, I tried to show how spring, hibernate, jsf, richfaces can be used together
as a working example. Sources can be downloaded at the end of page.
I am working about 3 days to configure Spring, Hibernate, JSF, Facelets, Richfaces all in one
as a maven project with maven jetty plugin. Now it is time to share this configuration as a
working example. First, lets look at root pom.xml.
ROOT
• pom.xml
We have 2 project. One of them is core and the other is web. Nothing is interesting here.
Core Project
• pom.xml
• dao-context.xml
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>blogspot.sezera.exampleproject.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.max_fetch_depth">1</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
<prop
key="hibernate.default_schema">exampleproject</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
• User.java
package blogspot.sezera.exampleproject.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="User")
public class User implements Serializable{
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
User class is a basic entity with email, password fields and accessor methods. id attribute is
defined as unique identifier for User entity by @Id annotation. Here ids are generating and
managing by hibernate. Accessor for id is getId() method. @GeneratedValue annotation can
be used for defining id generation strategy but we leave it default here.
• main-context.xml
<!--TRANSACTIAN MANAGEMENT-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the userManagementService interface -->
<aop:config>
<aop:pointcut id="managementServiceOperation"
expression="execution(*
blogspot.sezera.exampleproject.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="managementServiceOperation" />
</aop:config>
</beans>
• UserService.java
package blogspot.sezera.exampleproject.service;
public interface UserService {
void createUser(String username,String password);
}
• UserServiceImpl.java
package blogspot.sezera.exampleproject.service.impl;
import org.hibernate.SessionFactory;
import blogspot.sezera.exampleproject.dao.GenericDaoImpl;
import blogspot.sezera.exampleproject.domain.User;
import blogspot.sezera.exampleproject.service.UserService;
Here Dao pattern is used with generics support. A GenericDaoImpl instance is created for
User entity and persisted with makePersistent method which is actually a single line:
getSession().saveOrUpdate(entity).
• GenericDao.java
package blogspot.sezera.exampleproject.dao;
import java.io.Serializable;
• GenericDaoImpl.java
package blogspot.sezera.exampleproject.dao;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Now we have everything in core side to create a User. So moving to web project.
Web project
• pom.xml
<?xml version="1.0"?>
<project>
<parent>
<artifactId>exampleproject</artifactId>
<groupId>blogspot.sezera.exampleproject</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>blogspot.sezera.exampleproject.web</groupId>
<artifactId>exampleproject.web</artifactId>
<packaging>war</packaging>
<name>exampleproject.web Maven Webapp</name>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<finalName>exampleproject.web</finalName>
<!--MAVEN JETTY PLUGIN-->
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>blogspot.sezera.exampleproject.core</groupId>
<artifactId>exampleproject.core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_02</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2-b19</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repository.jboss.com</id>
<name>Jboss Repository for Maven</name>
<url>http://repository.jboss.com/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
• UserController.java
package blogspot.sezera.exampleproject.controller;
import blogspot.sezera.exampleproject.service.UserService;
public UserController(){
}
public String getUsername() {
return username;
}
• EmailValidator.java
package blogspot.sezera.exampleproject.validator;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
}
}
To validate user email address when creating user I have created a basic EmailValidator. It
validates if user email address contains "@". If not a ValidatorException is thrown.
• messages_en_US.properties
• newUser.xhtml
When user started to write his email address, page send an AJAX request to server in every
300 second and EmailValidator executes. If there is an error in user email, page renders an
error message right of user email input text area.
• web.xml
<?xml version="1.0"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd">
<display-name>ExampleProject Web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/main-Context.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>classic</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
<param-value>com.sun.facelets.FaceletViewHandler</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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<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>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
• faces-config.xml
<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_1_2.xsd"
version="1.2">
<application>
<resource-bundle>
<base-name>messages</base-name>
<var>msgs</var>
</resource-bundle>
<locale-config>
<default-locale>en_US</default-locale>
</locale-config>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
<validator>
<validator-id>emailValidator</validator-id>
<validator-class>
blogspot.sezera.exampleproject.validator.EmailValidator
</validator-class>
</validator>
<managed-bean>
<managed-bean-name>userController</managed-bean-name>
<managed-bean-class>
blogspot.sezera.exampleproject.controller.UserController
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>service</property-name>
<value>#{userService}</value>
</managed-property>
</managed-bean>
</faces-config>
NOTE:Attention to url extension is .faces NOT .xhtml. It must be same as url-pattern of Faces
Servlet Mapping in web.xml.
SCREENSHOT
DOWNLOAD
REFERENCES
AUTHOR
Sezer Akar
Blog <http://sezera.blogspot.com>