This article is a reference on how to enable Spring MVC framework for Java EE applications through web deployment descriptor file (web.xml). This is the first step to begin developing a Spring MVC – based application. Let’s look at how Spring MVC works in the following diagram:

Spring MVC workflow diagram

Spring MVC provides a dispatcher servlet which receives incoming requests and routes them to appropriate controllers. So it requires declaring this dispatcher servlet in web.xml file and configuring URL mapping for the servlet.

Declare the dispatcher servlet as follows:

<servlet>
	<servlet-name>SpringController</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-mvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

Spring’s dispatcher servlet is implemented by the class org.springframework.web.servlet.DispatcherServlet. The initialization parameter contextConfigLocation tells Spring where to load configuration files. The <load-on-startup> tag tells the servlet container to load this servlet upon start up with highest priority.

Configure the servlet mapping as follows:

<servlet-mapping>
	<servlet-name>SpringController</servlet-name>
	<url-pattern>*.htm</url-pattern>
</servlet-mapping>

That tells the container to route all requests end with htm to Spring’s dispatcher servlet. For example, the following URL will be processed by the dispatcher servlet:

http://localhost:8080/MySpringApp/hello.htm

Therefore we have a typical configuration for Spring MVC in web.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>Spring MVC App</display-name>

	<servlet>
		<servlet-name>SpringController</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>SpringController</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
</web-app>

 

Related Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#2Akshma Gandotra2019-07-11 04:29
Very precise and understandable.
Quote
#1afaf2017-06-03 23:18
very small content. Can you please elaborate more... with some more example...
Also what is the point of having *.htm. and not /
Quote