Spring MVC Hello World

Follow Steps as

  • Go to File
  • New Maven Project
  • Move to Archetype
  • Select webapp
  • Enter group id & artifact id

Now project has appeared in the project explorer.

Open pom.xml by double click go to the last tab of that add following dependencies:

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->

Right click on project go to maven and update project.

Then open web.xml and add following in it



As you can see I have added a servlet mapping here which is named as sample which  is dispatcher servlet of spring module

I have done URL mapping to /

Now create a sibling file of web.xml named as sample-servlet.xml (because sample is name of our dispatcher servlet) and add following in it

<beans xmlns="http://www.springframework.org/schema/beans"

   <context:component-scan base-package="com.sam" />

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

In this config file of spring, we have done following

  • Base package mapping to get component’s of spring like controller, service etc
  • View Resolver which will provide view to our requests.

Now create a controller in com.sam or any sub named as HelloController which will look like

package com.sam.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class HelloController {

    public ModelAndView handle(){
        return new ModelAndView("hello");

Here we have done request mapping of URL /hello so if someone is requesting this URL it will come to this functions.

This method is returning a ModelAndView object which is consists of model which is data we want to send as response and View which is file name so that view resolver use that name to get the view and spring will put data in that view and send response.

Now let’s create a folder view under webapp and a file named as hello.jsp which will look like this :

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">



Now add tomcat and run project.

Once your project started properly then hit hello url with project context.

for example:



4 thoughts on “Spring MVC Hello World

Add yours

  1. Well one of the things that I really liked about the Internet is the Freedom of Speech that it can allow you. You can get your point of view out to millions which in the past was solely the domain of MainStream professional Media outlets.Unfortunately just as quikly as we have gained this access we have various Users trying to restrict our Freedom of Speech to meet their own standards and personal agendas…..an unfortunate but mostlikely unavoidable outcome and of course you at YouTube of folded under the pressure…..truly a shame!


  2. / Quantas incriveis e revolucionárias reliquias arqueológicas não devem estar soterradas nos fundos dos mares e nas areias dos desertos apenas para o homem moderno encontrar… e se surpreender com novas revelações do seu passado!Gostei deste comentário ou não: 0


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: