• Basic Spring MVC Project By Maven

    Posted on December 26, 2015 by Hamidul Islam in Spring MVC.



    Any one who are in java world know more or less about Spring MVC. In this article we will create very basic Spring MVC project by Maven. This is just a quick start guide for those who want to starts Spring MVC. However learning Maven is not the scope of this article.


    To follow this article you should have basic knowledge of Java Web Application and Maven

    Step 1: Open your command prompt and execute the below command

    All Should Go in Single Line

    mvn archetype:generate

    Note 1: Once the above command is executed it will create below project structure


    Note 2: The above project is a simple java web application and its not a spring project. Below steps will make it Spring MVC project

    Step 2: Add Spring Dependency in pom.xml


    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <name>HelloWorldSpring Maven Webapp</name>
    <!-- Spring dependencies -->



    Step 3: Configure dispatcher servlet in web.xml


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring Hello World Example</display-name>



    Note 3: Every Spring MVC Project must have DispatcherServlet. Its a front controller in Spring Framework. Every requests goes through this front controller.

    Note 4: Look back Step 3. We have given name of the dispatcher servlet as dispatcher. So by default it will try to load dispatcher-servlet.xml file under WEB-INF directory. Similarly if the name of the dispatcher servlet is foo then it will try to load foo-servlet.xml and so on.

    Step 4: Create dispatcher-servlet.xml in WEB-INF directory


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="com.proliferay.controller" />

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


    Note 5: In Spring controller we generally return logical name of the views. For example if a controller return a string "hello" then spring will try to resolve this logical view to physical file. On the basis of the above configuration in Step 4 it will look for hello.jsp under /WEB-INF/views. Similarly if the controller returns a string "helloworld" then it will look for helloworld.jsp /WEB-INF/views and so on.

    Step 5: Create views folder under WEB-INF. The views folder contains helloworld.jsp


    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>HelloWorld Page</title>
    <h2>Hello World Page</h2>

    Step 6: Add  index page under webapp


    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Index Page</title>
    <h2>Hello World</h2>
    <a href="hello">Show Hello World Page</a>

    Step 7: Add the controller


    package com.proliferay.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    * @author Hamidul Islam


    public class HelloWorldController {

    public String showHelloPage() {
    System.out.println("#############showHelloPage method#############");
    * Spring will display helloworld.jsp under /WEB-INF/views

    return "helloworld";

    Running the example:

    Download the source code from the below link and create the war file of the project by below command

    mvn clean package

    Download Source Code

    Post Tagged with
Comments are closed.
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.