Introduction
Struts 2.0 is the popular Open Source Presentation Tier framework developed by Apache Group. It is based on MVC Model 2 design pattern. Dispatcher Filter is the front controller for the struts2 based applications. Struts 2.0 has simplified web development for its users by introducing POJO based actions, interceptors, flexible validation and support for many different result types.
Struts can be used to build the user interface tier of the enterprise application. Whereas, any of the popular ORMs like Hibernate, JPA, iBatis can be used for the persistence tier of the application. Struts2 provides easy integration with these persistence tier frameworks.
This article demonstrates the integration of Struts 2.0 applications with the Java Persistence API (JPA). The concept is explained with the help of a sample application. Knowledge of JPA and Struts 2.0 is the prerequisite for this article.
Development Environment
- NetBeans IDE 6.8
- GlassFish V2.x
- Apache Derby Database
Project Structure
The sample application developed in this article is “StrutsJPADemo” where an employee details are persisted to the database with the help of JPA.
Libraries/Jar Files Required
- Struts 2.0 jar files
- JPA jar files
- Jar file for the database driver (derbyclient.jar, in our case) The
complete application structure is shown below: 
- The User Interface (JSP pages) is created in the “Web Pages” directory. The java classes (Actions, Entities, Service classes, Resource Bundles, struts.xml) are created in the “Source Packages” directory. The required jar files are present in the “Libraries” directory. The web application deployment descriptor “web.xml” is created by the IDE in the “WEB-INF” subdirectory of “Web Pages”. This sample application stores the employee details into the Derby database.
Environment Set Up
Once the web application is created and all the required libraries are added to the classpath, we can start working on the different components of the application.
Before we start working on the different application components like JSP pages and Java classes, we must first configure the following resources in the application server:
- javax.transaction.UserTransaction
- Connection Pool & DSN
- For the database, we have created a connection pool in the application server to connect to ScrambleDatabase in the local machine running at port number 1527. The connection pool is bound to the name
“scramblePool” as shown in the snapshot below.
- The next step is to configure the DSN in the application server. Our DSN is bound to the name “jdbc/scramdleDSN” in JNDI. This is shown below in the snapshot. This DSN will use “scramblePool”created in the previous step.
- For the database, we have created a connection pool in the application server to connect to ScrambleDatabase in the local machine running at port number 1527. The connection pool is bound to the name









February 28, 2011
JPA, Struts 2.0