Struts 2 Spring 4 Integration With Spring JDBC

Prerequisites

Create Database

In this example, I just use the default test database. Now we need to create user table:

Step 1 Create New Maven-based Dynamic Web Project

Step 1.1 Create New Project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type Struts2Demo or whatever name you want in the Project name field.

Step 1.2 Configure Target Runtime

  • Click New Runtime… button under Target Runtime.
  • Select Apache Tomcat v7.0. Click Next.
  • Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
  • Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder of JDK installation. Select the folder then click Ok. Click Finish.
  • Check the JDK then click Ok.
  • Change JRE to the JDK then click Finish.
  • Click Finish to finish the project creation.

Step 1.3 Convert The Project To Maven Project

Right click to the project; select Configure->Convert to Maven Project. Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then paste the content below.

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 2 Create Web Deployment Descriptor (web.xml)

You can create web.xml when creating project or do it later. Right click to the project; select Java EE Tools -> Generate Deployment Descriptor Stub to generate web.xml.

Step 3 Create Application Context Configuration File

  • Right click to WEB-INF folder; select New -> Other…
  • Select XML File under General category and click Next.
  • Name it applicationContext.xml then click Finish.

Step 4 Struts Configuration File

  • Right click to WEB-INF folder; select New->Folder and name it classes.
  • Right click to the folder; select New-> Other… Select XML File under XML category and click Next.
  • Name it struts.xml then click Finish.
  • Replace its content by the following:

Step 5 Create JDBC Properties File

  • Right click to WEB-INF\classes folder; select New-> Other…
  • Select File under General category
  • Click Next.
  • Name it jdbc.properties
  • Click Finish.

Step 6 Create Java Bean Class

Right click to the project or src folder; select New->Class with the content as bellows:

Note: simply copy the code above to clipboard, select src folder of your project in Eclipse IDE then press CTRL + V.

Step 7 Create DAO Classes

Create package name info.java.tips.dao. Under the package, create DAO interface & its implementation.

Step 7.1 Create UserDao Interface

Step 7.2 Create DAO Class

Step 8 Create Service Classes

Create package name info.java.tips.service. Under the package, create UserManager interface & its implementation.

Step 8.1 UserManager Interface

Step 8.2 UserManagerImpl Class

Step 9 Create Struts Action Class

Right click to the project or src folder; select New->Class.

Step 10 Create JSP Pages

Step 10.1 search.jsp

To create JSP file, simply follow the following steps:

  • Right click to the project or WebContent folder, select New -> JSP File from the menu.
  • Type search.jsp in File Name field or choose any name you want then click Finish.
  • Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the following code:

Step 10.2 listUsers.jsp

Similarly, create the second JSP file:

Step 11 Clean & Build

Clean & build the project to clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 12 Configure Tomcat

Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.

If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.

Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.

Step 13 Deploy The Application

  • Right click to the Tomcat Server select Add and Remove.
  • Move the application (Struts2Demo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.

Step 14 Start Tomcat Server

Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.

Step 15 Run The Application

We assume that you install tomcat on default port (8080) and project name is Struts2Demo. Open web browser and type:

http://localhost:8080/Struts2Demo/search.jsp

Enter name to search; n for example:

Output

Note: in this example, I just implement list of user. For insert, delete & update functions. You may refer to our previous lesson http://javatips.info/spring-mvc-with-dbcp.html