Hello World Example with xml configuration Spring MVC in Eclipse

In the previous tutorial, we learn Hello World example using annotation driven configuration. In this post we will go farther and learn how xml based configuration will take place in the previous hello world example to do the same work with Spring 3 MVC. xml based configuration are old but most liked confutation technique but with the release of spring 3 , the spring developers pay more attention in annotation driven configuration due to the flexibility in it.

We have to do following changes in our previous example to make it xml configuration :

  1. We have to change the HelloWorldAction.java to remove annotations from code and the class will extend AbstractController class provided in spring 3 mvc jars.
  2. Also have to change app-config.xml to remove annotation driven configuration and put xml configuration for the controller HelloWorldAction.


The following will be the code of HelloWorldAction.java after changing the code for making xml configuration to work :

package com.raistudies.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldAction extends AbstractController{

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("HelloMessage", "Hello World from My First Spring 3 mvc application with xml xonfiguration");
        return mv;

Here, our HelloWorldAction class extends AbstractController class, that makes our class work as a controller and able to handle a request. We have override the handlerRequestInternal method which contains the login to set “hello.jsp” as view and a single model attribute with name “HelloMessage”. The changes are pretty much easy.


In the file app-config.xml, we have removed annotation driven configuration and our HelloWorldAction class as a bean as follows :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean name="/hello.htm" class="com.raistudies.actions.HelloWorldAction"/>
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>

Our controller class has been registered as a bean class with name “/hello.htm“. Here, by default spring 3 mvc will use BeanNameUrlHandlerMapping to resolve the url to controller class mapping. So, our controller will be associated to the url “/hello.htm“. The view resolver will work as usual and resolve jsp pages from view name.

With these changes, our hello world example will became xml based configuration. The output will be as follows :

Hello World with Spring 3 MVC and xml configuration

Hello World with Spring 3 MVC and xml configuration

You can download source code or war file from following links :

Code : Download

War : Download

Be with us :). Will introduce a lot of new things in Spring MVC. Your feedback will make us perfect as per your need, so do feedback.

Related Posts:

Leave a comment ?


  1. excellent job,very impressive.you have done a quite good job.I wondered about that others have not thought about it in such a way.Really informative post..Keep up the good job!!!Really impressed.This has helped me alot.Thank u so much and keep it up..Great job!!!

  2. Thanks for the tutorial!! I have seen spring framework in my courses very quickly reaching advanced level and definitely your tutorial explains many of the loose ends I had.

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>