This Spring MVC Tutorial explains Spring MVC application creation step by step. Whenever we start with any new technology, the first step we have to complete is – create a basic project structure and make that technology work. From detailed documentation of the technology, it becomes difficult to find out the first basic building blocks required. This tutorial takes the reader through the elementary steps to create a working Spring MVC project in Eclipse IDE and make it work on Tomcat. The technology involved are – Eclipse IDE (3.4.x), Tomcat (6.0), Java (1.5), Spring (2.5.5) and dependencies of Spring.

Create new Dynamic Web Project in Eclipse IDE:

Select appropriate target run time in second (New Dynamic Web Project) screen. Change the content directory and java source directory as shown. If you are not able to see the ‘Dynamic Web Project’ option in new project dialog box, then install updates for ‘Eclipse Platform’. On click of finish button, Eclipse will create a new project in the workspace.

 Spring MVC Tutorial

Spring MVC Tutorial

Spring MVC Tutorial

Adding Libraries for Spring MVC Tutorial:

Add following jars to the WEB-INF/lib directory.

aopalliance-1.0.jar
commons-logging-1.1.1.jar
servlet-api-2.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-context-support-2.5.5.jar
spring-core-2.5.5.jar
spring-web-2.5.5.jar
spring-webmvc-2.5.5.jar

Adding Libraries to Project:

Refresh Eclipse project and add the libraries to classpath as shown below.

Spring MVC Tutorial

Modifications to ‘web.xml‘:

Add following elements to the web.xml. Note – the servlet-name and url-pattern elements. Value of ‘url-pattern’ will be used in the url used to access pages. Value of element ‘servlet-name’ will be used to search the beans related to project in file ‘<servlet-name>-servlet.xml’.

      <servlet>
            <servlet-name> SpringMVCTutorial</servlet-name>
            <servlet-class>
              org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>SpringMVCTutorial</servlet-name>
            <url-pattern>*.htm</url-pattern>
      </servlet-mapping>

Create ‘SpringMVCTutorial-servlet.xml’:

Create the file as below. This will contain the controller definitions. (There is no restriction that it should contain only controllers, but from maintenance point of view, it is better to group bean definitions in relevant files.

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/helloworld.htm" class="com.myorg.springmvctutorial.web.controller.HelloWorldController"/>
</beans>

Create ‘HelloWorldController.java‘:

This class contains controller implementation related to our HelloWorld functionality. This also integrates with Spring MVC framework through the class that it implements. This class uses simplest implementation of controller by using org.springframework.web.servlet.mvc.Controller class. More featured implementations are also available as a part of framework.

package com.myorg.springmvctutorial.web.controller;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;

public class HelloWorldController implements Controller {
  protected final Log logger = LogFactory.getLog(getClass());
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    logger.info("Return View");
    return new ModelAndView("helloworld.jsp");
  }
}

Create View helloworld.jsp:


This can be a simple jsp to show that our code is working fine.

<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>This is Spring MVC Tutorial</h1>
<p>Our Tutorial is successful.</p>
</body>
</html>

Add the Project to Tomcat Server:

Right click on SpringMVCTutorial project in eclipse and select ‘Run As à Run on Server’ option. Select the server and click finish. The application will be deployed on selected server.

Spring MVC Tutorial

Accessing the Spring MVC Tutorial Application:

Our application can be assessed using Url – http://localhost:8080/SpringMVCTutorial/helloworld.htm

Spring MVC Tutorial

Complete source code of the application (without libraries) can be found here.

Additional Articles You may find Interesting:

Spring MVC Tutorial With Command Object on Eclipse and Tomcat

Complete Hibernate Tutorial with Example

A Visit to Kailasa Temple at Ellora Caves

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS