Spring MVC

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 SpringMVC 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 JSP Page

Right click to the project or WebContent folder; select New -> JSP File

Step 5 Create Controller Class

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

Step 6 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 7 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 8 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/SpringMVC/hello.do

Output