Friday , February 23 2018
Home / Spring / Spring MVC HelloWorld

Spring MVC HelloWorld

MVC stands for Model View Controller. The Spring MVC framework is one of the popular frameworks across all the frameworks available in the market. Because the advantages of Spring MVC framework tells the importance of Spring MVC.

In this tutorial we are going develop the step by step Spring MVC Helloworld Example using Netbeans.

Spring MVC Helloworld Example:

Step 1: Create a new Project in Netbeans by selecting the new project in File menu and select the web application in the choose project pop-up and click next.

Spring MVC I

Step 2: Give the project name as you want, For now I am giving SampleSpringApplication. Choose the project location where you want to save the project. By default it is in Netbeans projects in your home directory.

Spring MVC II

If you want to place all dependencies (libraries) in one folder, you can check the Use Dedicated Folder for Sorting Libraries option. Then click next.

Step 3 : Select the application server and J2EE version from the drop-down menu. And select next.

Spring MVC III

Step 4: Select the Spring Web MVC frame work from Frameworks menu. You can also select the version of the selected framework in libraries tab. If you want to include the JSTL in your application, you can select the JSTL check box. And click finish.

Spring MVC IV

Step 5 : Yes, we are done. By clicking on the finish button, netbeans will provide us the basic spring application architecture like below.

Spring MVC V

Step 6: Update the diapatcher-servlet.xml like below.

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns=""
    <bean class=""/>
    <context:annotation-config />
    <context:component-scan base-package="controllers" />
    <mvc:annotation-driven />

    <bean id="viewResolver"
          p:suffix=".jsp" />

Step 7: Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Step 8: Create controllers package in Source Package. And Create in controllers package.

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(ModelMap modelMap) {
        System.out.println("on method");
        modelMap.put("printme", "Hello Spring !!");
        return "index";

Step 9: Modify index.jsp like below,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
        <div style="background-color: green;height: 200px;width: 500px">
            <h1 style="color:black;"> ${printme}</h1>

Step 10 : Modify the redirect.jsp like below,

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<% response.sendRedirect("helloworld.htm"); %>

Well, it DONE !!

To run the application

Out put:

Spring MVC Helloworld

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


Spring MVC Tiles Example (Apache Tiles)

In this tutorials, I am going to show you how to work with Spring MVC …


    Huseyin Emre Arikan

    Thanks for the note. Two humble remarks for improvement:
    1.A tree view after creating the new files would be helpful, so that everyone knows where exactly in the file directory the new files are to be created.
    2.Not the end state of the default files but what is specifically edited (before and after)? That would I have wanted to know.
    Have a great day!


    Thanks for you lot. This is really help to me start learn Spring framework.


    Nice article. Very helpful. I am having some doubts regarding the request flow.
    As per my understanding, request flow is as follows (Please correct me if any thing is wrong) :
    1. Request url is containing ‘helloworld.htm’.
    2. Web.xml tell that any request that contain ‘*.htm’ should be sent to servlet named ‘dispatcher-servlet’
    3. Dispatcher servlet has ‘context : component scan’ . using this, spring understands that there must be some controller class for which there is @Controller annotation for it and @Request Mapping annotation is added with value = ‘helloword.htm’. i.e. all requests with such url pattern should be re-directed to this controller.
    4. Request will check for default method with annotation ‘@RequestMapping(method = RequestMethod.GET)’
    5. Response of this method will be returned.

    My doubts:
    1. How it is determined that response of method ‘helloWorld’ will be returned to index.jsp
    2. What if I want to redirect the response from this method to some other jsp file ?
    3. If I am having multiple methods in controller class, and want to call different methods in different situations, how would I manage it ?
    4. Does the sequence of ‘xmlns:context’ and ‘xmlns:mvc’ matters ?

    Thanks In Advance !


    Hi I’m getting a Error 404
    No mapping found for HTTP request with URI [/SampleSpringApplication/helloworld.htm] in DispatcherServlet with name ‘dispatcher’

    Did I Do something wrong?

    Vikum Samaranayake

    Thanks. 😀 Helps a lot. Keep it up


    Thanks a lot, please provide simple more examples on this and how configuration file works


    The only full working tuto. Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *