Struts 2.0 and JPA Integration

February 28, 2011

JPA, Struts 2.0

«»

Struts 2.0 and JPA Configuration Files

    • Here’s the configuration added in the “web.xml” file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
	<web-app version="2.5" 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-app_2_5.xsd">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<persistence-context-ref>
		<persistence-context-ref-name>
			persistence/myStrutsJPA
		</persistence-context-ref-name>
		<persistence-unit-name>
			StrutsJPAPersistenceUnit
		</persistence-unit-name>
	</persistence-context-ref>
	<resource-env-ref>
		<resource-env-ref-name>UserTransaction</resource-env-ref-name>
		<resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
	</resource-env-ref>
	<resource-env-ref>
		<resource-env-ref-name>jdbc/scrambleDSN</resource-env-ref-name>
		<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
	</resource-env-ref>
	<session-config>
		<session-timeout>
			30
		</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>RegisterEmp.jsp</welcome-file>
	</welcome-file-list>
	</web-app>

In the deployment descriptor, we have added the following configurations:

      1. The Welcome File of the application
      2. Struts2 Dispatcher Filter (Controller element of Struts2
        applications)
      3. UserTransaction
      4. PersistenceUnit
    • Here’s the configuration added in the “struts.xml” file kept in the default package in the “Source Packages” directory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE struts PUBLIC
		"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
		"http://struts.apache.org/dtds/struts-2.0.dtd">
	<struts>
	    <package name="default" extends="struts-default">
		<action name="addEmployee" class="com.actions.EmployeeAction">
			<interceptor-ref name="exception"/>
			<interceptor-ref name="alias"/>
			<interceptor-ref name="servletConfig"/>
			<interceptor-ref name="prepare"/>
			<interceptor-ref name="i18n"/>
			<interceptor-ref name="chain"/>
			<interceptor-ref name="debugging"/>
			<interceptor-ref name="profiling"/>
			<interceptor-ref name="model-driven"/>
			<interceptor-ref name="params"/>
			<interceptor-ref name="conversionError"/>
			<interceptor-ref name="validation"/>
			<interceptor-ref name="workflow"/>
			<result name="success">RegEmp_Success.jsp</result>
			<result name="error">RegisterEmp.jsp</result>
			<result name="input">RegisterEmp.jsp</result>  </action>
		</package>
	</struts>
    • The labels of various UI components on the JSP page are not hard coded and instead being derived from the resource bundle “ApplicationResources.properties” kept in the “com.messages” package in the “Source Packages” directory.

Here’s the code of “ApplicationResources.properties”
file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
app.title=Struts2 JPA Integration Application
	app.welcome=Welcome to Struts2 Application
	app.username=User Name
	app.city=City
	app.age=Age
	app.email=E-Mail
	button.save=Save
	app.username.blank=Please enter User Name
	app.email.blank=Please enter City
	app.age.blank=Please enter Age
	app.city.blank=Please enter city
	app.empid=Employee Id
	app.password=Password
	app.password1=Reenter Password
	app.empname=Employee Name
	app.doj=Date of joining

We configure “ApplicationResources.properties” as the default resource bundle for our application by configuring an entry in the “struts.properties” file kept in the default package in the “Source Packages” directory.

    • Here’s the configuration to be added in the “struts.properties”
      file:
1
struts.custom.i18n.resources=com.messages.ApplicationResources

Executing The Application using Struts 2.0 and JPA

Finally, we are ready to execute the application. Deploy the application to the container and execute. Depending on the configuration in the Persistence Unit to create/update/drop-create the database, the database schema is generated/updated.
In our application, we have set the configuration to create the database from scratch.Before executing the application here’s the database schema:No table with the name “Employee” is present.

Open the Register.jsp page.

Enter some invalid data, The snapshot given below shows the validation error messages

Once, all the validation error messages have been rectified, the user is forwarded to RegisterEmp_Success.jsp page, where the correct data submitted is displayed.

Check the database structure, by refreshing the database schema. A new table “Employee” is created with one record in the database.

Conclusion

Thus, any Struts application can seamlessly integrate with JPA easily.

email

«»

Comments

comments

,