Spring MVC XML Configuration Example

This tutorial shows how to build a basic web application using Spring MVC XML Configuration. We create a simple controller that’ll handle incomming requests and forwards the response to a simple view that’ll display a request attribute. We configure the DispatcherServlet in the web.xml deployment descriptor and configure spring mvc web application context using XML configuration.

Project Structure

Make sure your project looks similar to the following structure.

src
+--main
|    +--java
|        +--com
|            +--memorynotfound
|                +--controller
|                    |--HomeController.java
|    +--resources
|        |--app-config.xml
|        |--web-config.xml
|    +--webapp
|        +--WEB-INF
|            +--view
|                |--index.jsp
|            |--web.xml
pom.xml

Maven Dependencies

We are working with Apache Maven for our project management. Simply add the necessary dependencies to the project and Maven will resolve and manage all the dependencies automatically. Below is the pom.xml file for our project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.spring.mvc</groupId>
    <artifactId>java-config</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>SPRING-MVC - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>
    <packaging>war</packaging>

    <properties>
        <encoding>UTF-8</encoding>
        <spring.version>4.2.6.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- servlet api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Creating the Controller

The @Controller annotation indicates that this class serves the role of a controller. The DispatcherServlet will automatically discover this bean and map the corresponding methods annotated with @RequestMapping. The class-level @RequestMapping annotation maps to the root and will be used for the entire controller. The method-level @RequestMapping annotation further narrows the primary mapping for a specific HTTP request method. Inside the method we add a request attribute to the ModelMap which will be displayed in the view. The return value is the name of the view. The InternalResourceViewResolver will prefix and suffix the returned view and create the real location of the path.

package com.memorynotfound.controller;

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

@Controller
@RequestMapping("/")
public class HomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap model){
        model.addAttribute("message", "Spring MVC XML Config Example");
        return "index";
    }

}

Spring MVC XML Configuration

First we have the app-config.xml Spring Configuration file. We enable autodetection by registering the <context:component-scan/> element and provide the package to scan.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd ">

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

</beans>

Next, the web-configx.xml file will configure spring mvc. The <mvc:annotation-driven/> element will enable Spring MVC support. Finally, we register the InternalResourceViewResolver bean, which will resolve the returned view name to the real location of the view.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd ">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.memorynotfound" />

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

</beans>

Configuring the Dispatcher Servlet

The DispatcherServlet acts like a front-controller and is used to dispatch the requests to the appropriate controller methods. We register the servlet and provide the location of the web-config.xml xml configuration file using the init-param.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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_3_1.xsd" version="3.1">

    <display-name>Spring MVC XML Configuration Example</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:web-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Creating the View

Finally, we build a simple view displaying the value of the message attribute we added earlier in the controller.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Spring MVC XML Configuration Example</title>
</head>
<body>

    ${message}

</body>
</html>

Demo

URL: http://localhost:8081/spring-mvc-xml-config/

spring mvc xml configuration example

References

Download

You may also like...