Hello World (First) Spring MVC Project

Hello World with Spring MVC 4

Hi Readers, 

I am describing how to create your first Hello World application with Spring MVC. This tutorial is explained into steps that you need to follow. In this post all the steps are listed and you can perform these steps while watching the live action in given video.

Step 1 : create new dynamic web project

Start with a fresh dynamic web project. You can also start with Maven project and choose to skip step 2. In that case you need to select maven web project template.

Step 2 : convert that to maven project and give it group & artefact id

If you have created dynamic web project then it is time to use any build tool. We are using Maven here, you can also have build tool of your choice. Build tool is important here for few reasons
  • You can automate build of your spring project.
  • You can add dependancies to pom.xml file and need not to separately add jars and libs. This is much easier and smart way to work.

Step 3 : add spring MVC dependancy in pom.xml

This is minimal dependency you require to work with first Spring MVC application


Step 4 : open web.xml and create servlet & servlet mapping in servlet class give

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 id="WebApp_ID" version="3.1">




Step 5 : now create appname-servlet.xml file at root level inside web-inf folder

When your spring application will run, it will look for dispatcher servlet file inside WEB-INF folder. This is initial point of any spring application. We also call it front controller. Every request and response in spring mvc goes from this point. 

Step 6 : in your dispatcher servlet xml file write

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

 <context:component-scan base-package="com.techiekunal.helloworldweb.controller" />

  <property name="prefix">
  <property name="suffix">

 <mvc:annotation-driven />


In this you have defined base package to scan. So when a handler request will come it dispatcher servlet will look inside base package. Here we give our controller package name.

Here View Resolver is also added. It resolves view name passed by handler to actual file present in the system.

At last we declare that our application will be using annotations.

Step 7 : create a package and inside it, create a controller class. Add a handler to it.

In spring mvc we write handlers for each request. These handlers are written in controller classes.  So to start with we need one Controller class and handler of your request inside that class.

Step 8 : run application using tomcat.

You must have added tomcat to run your application. I will recommend using tomcat8. Right click on tomcat server and add your application. Server will start and your application will be deployed.