7/3/15

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

   <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

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"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 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">
 <display-name>helloworldweb</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>helloworldweb</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>helloworldweb</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>


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:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>

 <mvc:annotation-driven />

</beans>

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.

Done.