Spring MVC with DBCP

Step 1 Create Database

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

Step 2 – Create New Maven-based Dynamic Web Project

Step 2.1 Create new dynamic web project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type SpringMVCDemo for the project name (you can choose your own name).

Step 2.2 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 3 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 4 Create XML Bean Configuration file

In this example, I use Spring Tool Suite (STS) wizard to generate the file. You can just create an empty xml file named dispatcher-servlet.xml under package WEB-INF folder and paste the content below. The name of this file is the combination of the declared dispatcher servlet name in web.xml and the suffix “-servlet.xml”.

Step 5 Create properties files

Step 5.1 jdbc.properties

Right click to WEB-INF folder; select New->Folder and name it classes. Under the folder, create 2 properties file by select New-> Other… Select File under General category and name it jdbc.properties and paste the following content:

Step 5.2 messages.properties

Step 6 Create java bean class

Copy the below code; select your src folder in your project  and press CTRL + V to paste the code. Eclipse IDE will automatically create new class & package.

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 Controller Class

Copy the code below; Select src folder; press CTRL + V.

Step 9.1 List Users Controller

This controller displays list of users.

Step 9.2 Manage User Controller

This controller manages user details such as view, edit, delete…

Step 10 Create JSP Page

It’s good practice to put JSP files that just serve as views under WEB-INF, to hide them from direct access (e.g. via a manually entered URL). Only controllers will be able to access them then.

Right click to the project or WEB-INF folder; select New -> Folder and name it jsp. Under the folder create 2 JSP files:

Step 10.1 Create listUsers.jsp

Step 10.2 Create editUser.jsp

Step 11 Configure Tomcat Server

  • In Servers window, add new tomcat server; select Tomcat v7.0 Server then click Next. In case you don’t see the Servers window, you can go to Windows->Show View->Others… Select Servers under Server category.
  • In the next window, click Browse button to select Tomcat installation directory. Select the folder then click Ok.
  • Click Installed JREs, to configure configure runtime environment. Remove existing JRE then click Add button to add new one. Select Standard VM then click Next. Click Directory button to select JDK home folder; select the folder then click Ok.
  • Change the JRE to the JDK then click Finish.

Step 12 Deploy & Run

  • Go to Project -> Clean to clean & build the project. Eclipse will download & add maven dependencies to your project build path; then compile your classes.
  • Right click to the tomcat server; select Add & Remove. Move the application to the right to deploy it to the server.
  • Start the tomcat server
  • Open web browser and run the below URL. We assume that you install tomcat on port 8080 and context path is SpringMVC. By default project name is same as context path:

http://localhost:8080/SpringMVCDemo/listUser.do

Output

Click the link in Id columns to view detail & update (with underscore).

Output

Change name to ‘Mike T’ then click submit button

Output