How to configure Spring MVC using spring xml based configuration

This page lists down quick steps to set up Spring MVC in java web applications. On high level, we will take following steps:

  • Setup Spring root web application context
  • Setup Spring Web application context
  • Set up static resource handling
  • Set up JSTL view Resolver
  • Set up Spring annotations support

Setup Spring root web application context

As we know, Spring web application contexts are hierarchical. ContextLoaderListener creates a root web application context for the web application and puts it in the ServletContext. This context is generally used to load and unload the spring-managed beans.

WEB.XML
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
	classpath:spring-context-beans.xml
	</param-value>
</context-param>

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

Setup Spring Web application context

DispatcherServlet creates WebApplicationContext and handles application request flow using controllers/view-resolvers. When ContextLoaderListener is used along with DispatcherServlet, a root web-application-context is created first and a child context is created by DispatcherSerlvet and is attached to the root application-context.

WEB.XML
<servlet>
    <servlet-name>employee</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>employee</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Set up static resource handling

Add below code.

WEB.XML
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

Set up JSTL view Resolver

Add below code.

employee-servlet.XML
<?xml version='1.0' encoding='UTF-8'?>
<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-3.1.xsd'>

   <context:component-scan base-package='org.timesheet.web' />

   <bean
        class='org.springframework.web.servlet.view.InternalResourceViewResolver'>
        <property name='prefix' value='/WEB-INF/jsp/' />
        <property name='suffix' value='.jsp' />
    </bean>    
</beans>

Set up Spring annotations support

Add below code.

application-context.XML
<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<!-- Inform Spring to scan the annotation within codes for DI requirements -->
<context:annotation-config/>

 <!-- Inform Spring to scan the injectable components base on packages -->
<context:component-scan base-package="com.application.packagename" />
</beans>

Version History


Date Description
2014-02-16    Initial Version