Integrating JSF, Spring and JPA
October 3, 2007
In this blog we will see how we can integrate the power of JSF with spring to implement pagination.
Today J2EE developers have efficient framework like Java Server Faces (JSF) and Spring Framework available to them for application development.
On one side JSF has the capability to provide
1) Event driven programming model.
2) Navigation Handling.
3) Rapid application development approach.
While the Spring framework provides following features of
1) Dependency Injection – to provide loose coupling.
2) Light weight – Spring is lightweight in terms of both size and overhead.
3) Aspect-oriented - Spring comes with rich support for aspect-oriented programming (AOP) that enables cohesive development by separating application business logic from system services.
The idea is to use both JSF and Spring integrated together for application development such that we could utilize the features of both the framework in our application.
One application of such integration is providing the functionality of pagination
By a “pet catalog” sample J2EE Application we will look how we can integrate the two frameworks namely
JSF – Java Server Faces and Spring Framework and we will see how JPA – Java Persistence API can support the application in handling database related task.
We will have a look at a pet catalog J2EE web application for selling pets online
The example will demonstrate how we can implement dependency injection using spring framework in a JSF managed been, while using JPA to manage database activities.
Sample application source code can be downloaded from http://filesco.com/download.php?id=9C0D525D1
Important Files in the Application are explained as follows:
1) web.xml JSF Part in web.xml
apart from web.xml’s usual usage, we will use it for configuring the application to use JSF framework by writing
<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>
Spring Part in web.xml We will specify Spring configuration file(s). as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
To ensure that all of these configuration files are loaded, we’ll need to configurea context loader in your web.xml file.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
As we are not using the DispatcherServlet then we need to add a (small) bit of extra configuration to web.xml for scoped bean as follows
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
2) faces-config.xml
Following are the abstract of faces-config.xml
The easiest way to integrate one’s Spring middle-tier with one’s JSF web layer is to use the DelegatingVariableResolver class
The DelegatingVariableResolver will first delegate value lookups to the default resolver of the underlying JSF implementation, and then to Spring’s ‘business context’ WebApplicationContext. This allows one to easily inject dependencies into one’s JSF-managed beans.
<application>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>
We perform the job of injecting dependencies into one’s JSF-managed bean by #{catalogService} in the following abstract in faces-config.xml
<managed-bean>
<managed-bean-name>item</managed-bean-name>
<managed-bean-class>sessionpagination.ItemController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>catalogService</property-name>
<value>#{catalogService}</value>
<!– is a bean that is retrieved from the Spring ‘business context’. –>
</managed-property>
</managed-bean>
3) applicatonContext.xml We will load the Spring application context from this XML file. We will put all of our application’s <bean> definitions in this file, it makes sense for this file to contain <bean> definitions pertaining to controllers and other Spring components. Following are the abstracts taken from the application.context.xml file. <bean id=”catalogService” class=”service.CatalogDAO”/> Where catalogService is the bean id and service.CatalogDAO is the bean implementing class.
The bean catalogService is injexted in the JSF managed been “item” in faces-config.xml file as #{catalogService}.
The file contains other bean definitions pertaining to the controller.
4) Source Files-
- Model Classes
o Address
o Category
o InvalidItemException
o Item
o Product
o Sellercontactinfo
- Model classes use persistence api’s for data persistence
Javax.persistence.*;
- classes also use annotations e.g. @Entity
- Service
o catalogDAO(Implementing class)
§ Implementing persistence related operations using JPA.
§ PersistenceContext(refer persistence.xml)
o catalogService(Interface)
§ Defining persistence related operations.
- Sessionpagination
o ItemController
§ Is the implementation of a JSF managed been “item” which is available to all the jsp for pets(items
) navigation and item(pets) details.
§ This JSF managed bean has a property “catalogService” which is implemented by class “CatalogDAO”, the bean property is initialized by dependency injection feature of spring framework.
5) JSP Files\List.jsp
The file is used for listing various items(pets) information stored in the database, the information is retived using the JSF managed been “item” whose implementing class is ItemController.java.
Detail.jsp
This file also uses the JSF managed been item’s item property to display the details of the item(pet) selected.
6) Other Files
Other files include persistence.xml file of JTA framework
<?xml version=”1.0″ encoding=”UTF-8″?>
<persistence version=”1.0″ xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd”>
<persistence-unit name=”PetCatalogPu” transaction-type=”JTA”>
<jta-data-source>jdbc/PETCatalogDB</jta-data-source>
<properties/>
</persistence-unit>
</persistence>
We need to create a jta-data-source named “jdbc/PETCatalogDB” with the sun web server to access the derby database used in this example.
We use persistence unit name “PetCatalogPu” defined in persistence.xml in CatalogDAO.java to refer to the data base connection as follows :-
@PersistenceContext(unitName = “PetCatalogPu”)
Setup Note:
Use NetBenas to run the project,
Create a new J2EE project with existing source code.
Before running this example we need to setup the derby database and run the sql queries bundled with the source code.
(Also make sure that the persistence.xml is under the configuration folders.)
Saurabh Juneja can be reached at saurabhjuneja82@gmail.com
Entry Filed under: J2EE. .
Trackback this post | Subscribe to the comments via RSS Feed