Spring 4 MVC – A Simple Web Application

The Spring MVC framework provides a model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications.


Spring MCV basically consists of:

  • Model – Which Encapsulates the application data
  • View – Responsible for rendering the model data.
  • Controller – Processes user requests and builds appropriate model  and view.

The framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.

Here is a simple application to understand how Spring MVC architecture works:

  • Configure web.xml:
<?xml version="1.0" encoding="UTF-8"?>
        <display-name>Javabrown.com - Spring MVC Sample</display-name>


  • As per the servlet-name for spring, define mvc-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> 
   <context:component-scan base-package="com.javabrown.core.component" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 <property name="prefix">

	 <property name="suffix">

  • Define SampleController.java
package com.javabrown.core.component;

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

@Controller@ RequestMapping("/sample")
public class SampleController {

    @RequestMapping(method = RequestMethod.GET)
    public String hello(ModelMap model) {
        model.addAttribute("javabrown-msg", "Hello JavaBrown!");
        return "javabrown-msg-view";

  • Define javabrown-msg-view.jsp as a controller response
  <h1>Spring-4 MVC : javabrown.com</h1>
      <h3>SampleController sayes : ${javabrown-msg}</h3>