Spring MVC with XML Configuration

When we are making a Spring Framework web application, this Spring Framework is provided with the Spring MVC module. Some of the important points are:
     • Spring Framework for making web applications in java
     • It is based on the Model-View-Controller design pattern for making web applications
     • Supports features of the Core Spring Framework(IoC, DI)



→ The Front controller is called as a DispatcherServlet.
→ Model is the Java Bean class or POJO class.
→ Controller class is not containing the business logic of the application. It is having delegation capability only. 
→ View is a web page(JSP, Thymeleaf, etc.).    

Development Process

    1. Creating a Project
    2. Project Structure
    3. Adding the jar file 
    4. Creating a View page 
    5. Configuring Spring DispacherServlet
    6. Add configuration to file
    7. Creating a Controller Class
    8. Run the App

1. Creating a Project

We are creating a Project in the Eclipse IDE. You can create a project in othe IDE which you are using. 

2. Project Structure 

3. Adding the jar file


→ Paste these jars into the WEB_INF/lib directory of the project and these jars are automatically added to the Java Build Path/Classpath.

4. Creating a View page

We are creating jsp view page home.jsp for this project

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<h2>Spring MVC Demo - Home Page</h2>

→ This JSP page is created in the WEB-INF/view directory.

5. Configuring Spring DispacherServlet


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4.xsd"
id="WebApp_ID" version="4.0">

<absolute-ordering />
<!-- Spring MVC Configs -->
<!--  Configure Spring MVC Dispatcher Servlet -->
<!-- Set up URL mapping for Spring MVC Dispatcher Servlet -->

→ This web.xml file is created in the WEB-INF directory of the application.
→ Client requests are entered into the web application via FrontController. In the Spring MVC DispatcherServlet is the FrontController. This is the gateway only which does not contain real web application logic or business logic of the application. It is delegated to the POJO class as annotated with the @Controller annotation. The Controller class delegates to the Server class which contains business logic and delegates to the Repository class which contains persistence logic. Once work has been done, the FrontController is responsible for producing output as a view page (JSP page, Velocity template, or Thymeleaf template). The techniques are used to decide which Controller and which method inside that Controller contains which handles the request and renders the view page as a response.
→ DispatcherServlet is a class that is available in this package org.springframework.web.servlet.
→ This tag <url-pattern>/</url-pattern> means the FrontController is ready to handle that start from ” / “. We can change this according to our needs.
→ The DispatcherServlet initialises once when we are deploying the application on the tomcat server.
→ The <load-on-startup>1</load-on-startup> tag means DispatcherServlet initialized during the time of the server startup.

6. Add configuration to file


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<!-- Add support for component scanning -->
<context:component-scan base-package="com.poc" />
<!-- Add support for conversion, formatting and validation support -->
<!-- Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />

7. Creating a Controller Class


package com.poc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class HomeController {

public String showPage() {

return "home";


→ @Controller: Specialization of the @Component annotation, useful to configure classes as the controller of the Spring MVC app to process requests given by clients.
→ @RequestMapping: This is used to map the web requests onto specific handler classes and handler methods.

8. Run the App


You can check out this article Spring MVC with Spring Boot Configure to create web application in Spring Framework in easy way.


This example is explained How to configure a web application in Spring Framework? What is the use of the FrontController class in Spring MVC? What is the use of @RequestMapping annotation in the application?

Leave a Comment