Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring MVC 4 Example

Tags: import spring

Spring 4 Framework introduced with lots of improvements and features. Here we are going to deal with developing web application using Spring 4 Framework.

Project Requirements

  1. Spring 4.0.1.RELEASE jars
  2. JDK 1.6
  3. JSTL 1.2.jar
  4. Eclipse Java EE IDE(Eclipse JUNO)

Project Setup

Spring MVC 4 Example

Spring Configuration

Spring MVC 4 has replaced XML configuration with Java file configuration. You can use either one of them to build web application.

1.Configuring Spring Beans(spring-servlet.xml)

Spring MVC 4 facilitates by providing following annotations to define spring beans.

  1. @Configuration: It marks the file configuration file.
  2. @ComponentScan :It is used to scan the service classes.
  3. @EnableWebMvc : It is equivalent to which enables Web MVC environment.

The @Bean annotation is used to define spring beans here ViewResolver is annotated using @Bean annotation.

import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@ComponentScan(basePackages = {"com.expertwebindia.controllers"})
public class AppConfig {
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
return viewResolver;


The above configuration is same as below spring-servlet.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<context:component-scan base-package="com.expertwebindia.controllers"/>
<bean id="viewResolver"
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />

2.Configuring Dispatcher Servlet

As in Spring 3 we need to configure Dispatcher Servlet in web.xml. Now in Servlet 3.0 we can add servlet entry without web.xml.

package com.expertwebindia;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));

The below configuration is equivalent to above Java file configuration which are doing in web.xml.


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

3.Defining Spring Controller

Defining controller is same as Spring 3, using @Controller annotation.

package com.expertwebindia.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
public class HelloWorldController {
public String hello(ModelMap model){
model.addAttribute("msg","Hello World Example by Expertwebindia");
return "hello";


4.Defining Views(JSP files)

Here is one view used which hello.jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Web MVC 4 Example</title>

Download Source Code

You need to put Spring 4 Jars in WEB-INF/lib.

Download Spring MVC 4 Example

Run Application


This post first appeared on Expertwebindia|Online Web Solutions, please read the originial post: here

Share the post

Spring MVC 4 Example


Subscribe to Expertwebindia|online Web Solutions

Get updates delivered right to your inbox!

Thank you for your subscription