Sample Application using JSF, Spring 2.5, and Java Persistence APIs with Glassfish v2

Sample Application using JSF, Spring 2.5, and Java Persistence APIs with Netbeans 6.1 and Glassfish v2

You can dowload the Sample Application using JSF, Spring 2.5, and Java Persistence APIs  on Glassfish v2 and a related presentation  JavaServer Faces, Java Persistence API, Java EE, Spring, Seam.

A Summary of the Technologies and Frameworks in the Sample Application

If you're not familiar with JavaServerFaces technology, the Java Persistence API, or Spring, here are brief descriptions:

  • JavaServer Faces Technology (often referred to as JSF) is a server-side user interface (UI) component framework for web applications. It simplifies the development of sophisticated interactive web UIs by providing configurable, reusable, extendable UI components, support for event handling, input converters and validators, a navigation model, a component rendering model, and a managed bean model for translating input events to server-side behavior.

  • The Java Persistence API provides a (plain old Java object) POJO-based persistence model for Java EE and Java SE applications. It handles the details of how relational data is mapped to Java objects, and it standardizes Object/Relational (O/R) mapping.

  • Spring is a lightweight, POJO-oriented, open source framework for developing Java enterprise applications. Spring does not reinvent application server functionality such as connection pooling, or provide an object-relational mapping layer. Instead it provides support for Inversion of Control (IoC), dependency injection, Aspect Oriented Programming (AOP), and an abstraction/services layer designed to make existing Java Enterprise application server technologies easier and more transparent to use.

Explanation of the usage of JSF, Spring 2.5 , and Java Persistence APIs in a sample Store Catalog Application

The sample application is an online catalog which allows a user to page through a list of items in a store.


The List.jsp page uses a JSF dataTable component to display a table of catalog items.

The dataTable component is useful when you want to show a set of results in a table. In a JavaServer Faces application, the UIData component (the superclass of dataTable)  supports binding to a collection of data objects. The UIData component does the work of iterating over each record in the collection of data objects. The HTML dataTable renderer displays the data as an HTML table.

In the List.jsp web page the dataTable is defined as shown below:  (Note: Red colors are for Java EE tags, annotations code, Blue for Spring specific and Green for my code or variables)

Code Sample from:  List.jsp

<h:dataTable value='#{itemController.items}' var='dataTableItem'
 border="1"  cellpadding="2" cellspacing="0">

The value attribute of a dataTable tag references the data to be included in the table. The value attribute in the dataTable tag points to a list of catalog items identified by the expression #{itemController.items}. The value is bound to the items property of a managed bean that has the managed bean name itemController.

The var attribute specifies a name , dataTableItem,  which is used as an alias for a single item in the  dataTable.  In the dataTable tag from the List.jsp page, the value attribute points to a list of catalog items. The var attribute points to a single item in that list. As the UIData component iterates through the list, each reference to dataTableItem points to the current item in the list.

The dataTable's value is bound to the items property of the itemController ManagedBean.
This ItemController ManagedBean items property is defined as shown below:

Code Sample from:
    package controller;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component; 


  public class ItemController {

    public DataModel getItems() {
        if (model==null  || index != firstItem){
        return this.model;

    public DataModel getNextItems() {      
        model = new ListDataModel(catalogService.getItems(firstItem,batchSize));
        index = firstItem;      
        return this.model;

@Component is a Spring 2.5 "stereotype" annotation. @Repository, @Service and @Controller extend @Component and are role designations for a common three-tier architecture components (data access objects, services, and web controllers).  these stereotypes facilitate the use of Spring AOP and post-processors for providing additional behavior to the annotated objects based on those roles and can also be used for auto-detection of components on the classpath.

The @Scope("session") annotation binds a web-tier Spring-managed object to the specified scope. "session" scopes a  bean definition to the lifecycle of a HTTP Session.

The  Spring  2.5 component scanning functionality removes the need to define  JSF Managed Beans or Web tier "controllers"  in the faces-config.xml, and other components in the Spring applicationContext.xml. The following configuration is used to trigger the auto-detection of all components in the controller package:

Code Sample from: applicationContext.xml

To integrate Spring with JSF
configure the Spring  JSF 1.2 ELResolver that delegates to the Spring root WebApplicationContext, resolving name references to Spring-defined beans. Configure this resolver in your faces-config.xml file as follows:

Code Sample from: faces-context.xml


The ItemController getItems() method wraps a List of item objects, returned from the catalogService,  in a DataModel.
UIData, the superclass of dataTable, supports data binding to a collection of data objects represented by a DataModel instance.  The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a row index.  The APIs provide mechanisms to position to a specified row index, and to retrieve an object that represents the data that corresponds to the current row index.   

The Item properties Name, Photo, and price are displayed with the dataTable column component, which displays a table column:

Code Sample from: List.jsp
<h:dataTable value='#{itemController.items}' var='dataTableItem'
 border="1"  cellpadding="2" cellspacing="0">

      <f:facet name="header">
          <h:outputText value="Price"/>
      <h:outputText value="#{dataTableItem.price}"/>

The column tags represent columns of data in a dataTable. While the UIData component is iterating over the rows of data, it processes the UIColumn component associated with each column tag for each row in the table.

The UIData component  iterates through the list of items returned by ItemController getItems() , ( referenced by value='#{itemController.items}') ,  and displays the dataTableItem.price. Each time UIData iterates through the list of items, it renders one row  in each column.

The dataTable and column tags use facet to represent parts of the table that are not repeated or updated in the rows. These include headers, footers, and captions.

Auto-Detection of Spring Components

The CatalogDAO,  is defined as a Spring bean by the  @Repository stereotype which extends the @Component annotation and can be  used for auto-detection of components on the classpath.

Code Sample from:

package service;

public class CatalogDAO implements CatalogService {


The The Spring IoC container  will inject the catalogService Spring Bean into the catalogService property of the ItemController JSF ManagedBean :

Code Sample from:

  public class ItemController {

  private CatalogService catalogService ;

  public void setCatalogService(CatalogService catalogService) {
        this.catalogService = catalogService; 

@Autowired  marks a constructor, field, setter method or config method to be autowired by Spring's dependency injection facilities. @Autowired makes it possible to inject dependencies that match by type.   The setCatalogService is marked as @Autowired and accepts an argument of type CatalogService. This is Spring 2.5 annotation-driven dependency injection: This setter will be called passing in a Spring bean of type CatalogService, obtained by type from the Spring ApplicationContext.
To enable this, add this to the Spring applicationContext.xml:

Code Sample from: applicationContext.xml

<context:annotation-config/> <context:component-scan base-package="service" />

Using the Java Persistence API (JPA) with Spring 2.5

The Spring bean CatalogDAO uses an EntityManager Query object in the Java Persistence API to return a list of items. If you look at the source code for CatalogDAO , you'll notice that it annotates an EntityManager field with a @PersistenceContext annotation. This injects an entity manager into the Spring Bean  the same way that an Entity Manager is injected into an Enterprise JavaBeans Technology (EJB) session bean.

Code Sample from:
package service;
public class CatalogDAO implements CatalogService {

    private EntityManager em;

@Transactional(readOnly = true)
    public List<Item>  getItems(int firstItem,int batchSize) {      
Query q = em.createQuery("select object(o) from Item as o");
       List<Item> items= q.getResultList();
       return items;      

The createQuery method creates an instance of a Query class for executing a Java Persistence query language statement. The setMaxResults() method in Query sets the maximum number of results to retrieve, and the setFirstResult method sets the position of the first result to retrieve.

Item is an Entity class -- a typical Java Persistence entity object -- which maps to an ITEM table that stores the item instances. If you examine the source code for Item, you'll see that it meets the two requirements for an entity:

  • The class is annotated with an @Entity annotation.
  • The primary key identifier is annotated with an @Id annotation.

Code Sample from:

public class Item implements {

    private String itemID;

    private String name;   
    private String description;   
    private String imageurl;   
    private String imagethumburl; 
    private BigDecimal price;
    private Address address;
    private Product product;

    public Item() { }

    public String getName() {
        return name;

    public void setName(String name) { = name;


The ItemController ManagedBean pages through the list of Items by maintaining the firstItem and batchSize attributes and passing these as parameters to the CatalogService getItems(firstItem, batchSize) method, which gets the items for display in the table of pets.

The ItemController ItemCount  property is  used to get and display the number of Catolog items in the  data base on the List.jsp page:

Code Sample from: List.jsp

<h:outputText value="Item #{
itemController.firstItem +1} ..#{itemController.lastItem} of #{itemController.itemCount}"/>

This ItemController ItemCount property is defined as shown below:

Code Sample from:

    public int getItemCount() {      
        int count = catalogService.getItemsCount();
        return count;     

The ItemController getItemCount() method calls the CatalogService interface to get the count of the list of items. The CatalogDAO Spring bean  getItemCount() method uses the JPA Query interface to get the count of all items in the database item table:

Code Sample from:

public class CatalogDAO implements CatalogService {
  private EntityManager em;

. . .
@Transactional(readOnly = true)
  public int  getItemCount() {
Query q = entityManager.createQuery(
                 "select count(o) from Item as o");     
      int count = ((Long)q.getSingleResult()).intValue();
      return count;

Displaying the Next List of items

A JSF commandLink is  used to provide a link to click on to display the next page of items. The commandLink tag represents an HTML hyperlink and is rendered as an HTML <a> element. The commandLink tag is used to submit an action event to the application. 

Code Sample from: List.jsp

 <h:commandLink action="#{}" value="Next #{itemController.batchSize}"
    rendered="#{itemController.lastItem + itemController.batchSize <= itemController.itemCount}"/> 

This commandLink action attribute references the ItemController backing bean next() method which calculates the next page's first row number  and returns a logical outcome String, which causes the List.jsp page to display the next page's list . The ItemController next method is defined as shown below:

Code Sample from:

   public String next() {
       if (firstItem + batchSize < getItemCount()) {
           firstItem += batchSize;
       return "item_list";

The JavaServer Faces NavigationHandler matches the logical outcome,  item_list against the navigation rules in the application configuration resource file faces-config.xml to determine which page to access next. In this case, the JavaServer Faces implementation loads the List.jsp page after this method returns.

Code Sample from: faces-config.xml


When the List.jsp dataTable is rendered, the value binding causes the  ItemController getItems method to be called, as discussed before, which will cause the next list of items to be displayed. 
Code Sample from: List.jsp
<h:dataTable value='#{itemController.items}' var='dataTableItem'
 border="1"  cellpadding="2" cellspacing="0">

Displaying an Item's Details 

A JSF commandLink is  used to provide a link to click on to display a page with the item details. This commandLink action attribute  references The ItemController detailSetup() method:

Code Sample from: List.jsp

       <f:facet name="header">
          <h:outputText value="Name"/>
       <h:commandLink action="#{
itemController.detailSetup}" value="#{}"/>   

The ItemController detailSetup() method  gets the item data from the current row of the dataModel, and returns a string which causes the Detail.jsp page to display the item details :

Code Sample from:

    public String detailSetup() {
        item = (Item) model.getRowData();
        return "item_detail";

The JavaServer Faces NavigationHandler matches the logical outcome,  item_detail against the navigation rules in the application configuration resource file to determine which page to access next. In this case, the JavaServer Faces implementation loads the Detail.jsp page after this method returns.

Code Sample from: faces-config.xml


The Detail.jsp uses the outputText component to display the ItemController ManagedBean's item properties:

Code Sample from: detail.jsp

    <h:outputText value="#{}" title="Name" />
    <h:outputText value="#{itemController.item.description}" title="Description"/>
    <h:graphicImage url="#{itemController.item.imageurl}" title="Imageurl" />
    <h:outputText value="#{itemController.item.price}" title="Price" />
    <h:outputText value="#{item}" title="Address" />
    <h:outputText value="#{item}" title="Address"/>  


This concludes the sample application which demonstrates how to work with the JSF dataTable and  DataModel  to page through a list of  Item Entities which are retrieved using the Catalog methods which use  the Java Persistence APIs with Spring 2.5.

Running the Sample Code

The sample code for this tip is available as a NetBeans project. You can build and run the sample code using the NetBeans IDE.

Set up the Development Environment: (these directions are also here with screenshots)

  1. Download and install NetBeans 6.1 . Get the full distribution with Java EE and Glassfish v2.
  2. Install the Spring Framework Plug-in into the NetBeans IDE:
    • Select Tools from the NetBeans menu bar and select Plugins.
    • Select Available Plugins tab and then select Spring Framework Support and then click the Install button.
  3. Open the Admin Console of the GlassFish V2 application server in order to create the JDBC Connection pool and JDBC resource:
    1. Select the NetBeans Services tab window.
    2. Expand Servers.
    3. Right click GlassFish V2 and select View Admin Console.
    4. Enter values to User Name and Password fields - the default is admin for User Name and adminadmin for the Password
  4. Create a connection pool using the Admin Console:
    1. Expand Resources->JDBC.
    2. Click Connection Pools.
    3. Click New on the right of the page.
    4. For the Name field, enter PETCatalogPool.
    5. For Resource Type, select javax.sql.DataSource from the drop-down menu.
    6. For Database Vendor field, select JavaDB from the drop-down menu.
    7. Click Next.
    8. Scroll down to the end of the page to see the Additional Properties section.
    9. For Connection Attributes field, enter ;create=true.
    10. For DatabaseName field, enter pet-catalog.
    11. For Password field, enter app.
    12. For User field, enter app.
    13. Click Finish.
    14. Remove the SecurityMechanism property from the PETCatalogPool:.
      1. Select PetCatalogPool under Connection Pools node on the left.
      2. Select Additional Properties tab on the right and observe that the Edit Connection Pool Properties are displayed.
      3. Check Security Mechanism and click Delete Properties.Click Save.
  5. Start the Java DB server using NetBeans:
    1. Select Tools from the NetBeans top-level menu bar and select Java DB Database->Start Server.
  6. Make sure the PetCatalogPool is operational using the Admin Console:
    1. Start the Java DB server (if it is not started already)
    2. Select the Admin Console General tab window.
    3. Click Ping button.
    4. Make sure that the Ping Succeeded message is displayed on the top.
  7. Create a JDBC resource using the Admin Console:
    1. Select JDBC Resources under JDBC on the left of the Admin Console .
    2. Click New button on the right.
    3. For JNDI Name field, enter jdbc/PETCatalogDB.
    4. For Pool Name field, select PETCatalogPool from the drop-down menu.
    5. Click OK.
  8. Create a Database connection using NetBeans:
    1. Select the NetBeans Services tab window.
    2. Right click Database and select New Connection.
    3. Observe that the New Database Connection dialog box appears.
    4. For the Database URL field, enter jdbc:derby://localhost:1527/pet-catalog.
    5. For User Name field, enter app.
    6. For Password field, enter app.
    7. Click OK.
  9. Create a database using NetBeans:
    Select Tools from the NetBeans top-level menu bar and select Java DB Database->Create Database.
    1. Observe that the Create Java DB Database dialog appears.
    2. For the Database Name field, enter pet-catalog.
    3. For User Name field, enter app.
    4. For Password field, enter app.
    5. Click OK.
  10. Populate the database tables  in the pet-catalog database as follows:
    1. Under Databases, select the connection pet-catalog that you just created.
    2. Right mouse click on pet-catalog and select Excecute Command.
    3. In the SQL command window copy paste all the sql text from the file <sample_install_dir>/SpringJPA/setup/sql/javadb/catalog.sql,
    4. At the top of the window click on the icon for Run SQL. This will create all of the tables and data for the application.

Open and Run the Sample code:

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as <sample_install_dir>/SpringJPA, where <sample_install_dir> is the directory where you installed the sample package. For example, if you extracted the contents to C:\\ on a Windows machine, then your newly created directory should be at C:\\SpringJPA.

  2. Start the NetBeans IDE. Click Open Project in the File menu and select the SpringJPA directory you just unzipped.
  3. You will see a Reference Problems dialog when you open the project. That's because you need to add the Spring Framework to the project.   click Close in the dialog.
  4. Right click the SpringJPA project libraries node and select add Library, then select Spring Framework 2.5  (the Spring Framework Plug-in which you installed earlier), then click Add Library.

  5. Build the project as follows:

    • Right click the SpringJPA node in the Projects window.
    • Select Clean and Build Project.

  6. Run the project as follows:

    • Right click the SpringJPA node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the JSF, Java Persistence API, and Spring 2.5 Sample Application (at http://localhost:8080/SpringJPA/).

For additional information see:


Hello, I was wondering if you could tell me how to install "or" add Spring Security to my web applications using NB 6.5. I have the MVC 2.5 plug in, but that doesn't seem to have the Web Flow or the Security libraries. So when I go create a Spring MVC project, and then search the libraries, there's not any Web Flow or Security. Can you help me please? Thank you.

Posted by john on May 05, 2009 at 08:29 AM EDT #

See the video at the spring security home page:

Unfortunately, most Spring Sec tutorials are for Spring Project. If you do not want to use Spring, it is enough to add the jar files contained in the Spring Sec download + the Spring Web MVC jar files (the later are contained also in the sample projectst, but not in the dist of the downloadable Spring Sec.

Then some minor changes in your web.xml and creation of a SS-specific config file (copied from a sample project), and thats it. Then you can experience or implement specific things.

Posted by Geza on July 06, 2009 at 12:32 PM EDT #

Hi, I enjoyed the architecture that you used in this project, but when there is a need to deploy in the Tomcat server.
In glassfish I could deploy and run this example, but I really would like to execute them in Tomcat. Firstly, I would like to know if this is possible, because I tried for 8 hours for 3 days and I could not!

The closest I got to play perfect, was the launch of the exception ...

INFO: Illegal access: this web application instance has been stopped already. Could not load java.lang.Object. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
at org.apache.catalina.loader.WebappClassLoader.loadClass(

... on start and the exception ...

javax.servlet.ServletException: #{itemController.detailSetup}: java.lang.ClassCastException: model.Item cannot be cast to model.Item

... when I try view the details of a item.

I think the problem is the configuration:
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>

If possible I'd like to send you the war of this project (PETCatalog-SpringJPA) already with my amendments, in which I try to run it in tomcat. If you can help me, I would be very grateful.

Thanks you!

Posted by Flavio Sousa (BR) on November 15, 2009 at 01:47 PM EST #

I am getting same error as Flavio. I am using Spring batch with eclipselink

Following is my aplication context file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="jobLauncher" class="">
<property name="jobRepository" ref="jobRepository" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
<context:component-scan base-package="com.ezp.batch,com.fd.batch.table"/>
<bean id="main" class="com.ezp.batch.RunJob"></bean>
<bean id="entityManager" class="">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<bean id="placeholderProperties"
<property name="location" value="" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />

<bean id="jobRepository"
p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:isolationLevelForCreate="ISOLATION_DEFAULT"/>
<batch:job id="job1">
<batch:step id="step1">
<batch:tasklet ref="myTasklet"/>
<bean id="myTasklet" class="com.ezp.batch.ProjectTasklet"></bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

<bean id="entityManagerFactory"
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
<property name="persistenceUnitName" value="baPU"></property>


Posted by Maruthi Kakani on November 26, 2009 at 06:03 PM EST #

Post a Comment:
Comments are closed for this entry.



« July 2016