Creating a Spring MVC Simple Example

In this article we will learn how to create a spring mvc simple example.

Please install / setup the below softwares to get started
1. JDK 1.7
2. Maven
3. Tomcat

I will be using eclipse IDE.

1. Create a Maven project in eclipse

Create New Project and select Maven project and click next
Spring MVC Tutorial for Beginners

Uncheck the “Create a simple project” checkbox and click next
Spring MVC Tutorials with Examples

Filter the archetypes using “maven-archetype-webapp” and then select the available archetype
Spring MVC Examples for Beginners

2. Edit the pom.xml to add spring dependencies

Once the maven project is created , now edit the pom.xml to add spring dependencies

3. Create a package structure and a controller class

I created a package named com.kscodes.sampleproject and a controller class named FirstController.java

We have used the ModelAndView class that will return the view name along with the message that we will be displaying in the jsp page.
View name in the case is “firstpage”

4. Create a jsp folder and add a view file

Create a jsp folder inside the WEB-INF folder. Inside the jsp folder create firstpage.jsp file. This jsp will act as a view to display the message that we send through the FirstController.java

5. Create a spring configuration file.

Create a spring configuration file named “dispatcher-servlet.xml”

6. Modify the web.xml to create integrate spring.

 

Thats it. Build the project and deploy it in tomcat.

Output

Spring MVC Example

Download the Source – sampleproject.zip