Spring MVC XML Configuration Example





Hey guys in this post, you will understand about Spring MVC XML configuration with explanation.

Read More:

Configure the Dispatcher Servlet


Inside the web.xml, configure the Spring MVC Dispatcher servlet

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

Dispatcher servlet is just a Servlet, acts as a Front controller. It delegates the web requests to the appropriate controllers. It is already developed by Spring team.

Set up the URL mapping


Next step is to setup the URL mapping for Spring MVC dispatcher servlet.

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

We will configure all the web requests to a URL pattern ‘/’

Add a support for component scanning


The next step is to add support for component scanning.

<context:component-scan base-package="in.bushansirgur.springmvc" />

To load the spring components at the time of application load, we need to tell spring that scan the packages for creating an instances of components that we created. We will specify the base package name.

Add support for annotation


Next we will provide a support for conversion, formatting and validation support.

<mvc:annotation-driven/>

Define Spring MVC view resolver


The next step is the configure the view resolver for web templates

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

we will add the prefix that where the templates are present. Controller will send the view name, we will add the suffix to the view name. So we will get a complete path that we exactly the view templates are present.


You can copy the complete code for your reference –

web.xml

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

	<display-name>spring-mvc-demo</display-name>

	<absolute-ordering />

	<!-- Spring MVC Configs -->

	<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-mvc-dispatcherservlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

spring-mvc-dispatcherservlet.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: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/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- Step 3: Add support for component scanning -->
	<context:component-scan base-package="in.bushansirgur.springmvc" />

	<!-- Step 4: Add support for conversion, formatting and validation support -->
	<mvc:annotation-driven/>

	<!-- Step 5: 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" />
	</bean>

</beans>

That’s it for this post, if you like this post, please share this post with your friends and collogues and also share this on your social media profiles.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply