Configure Multiple Handler Mappings in Spring MVC

Spring supports a variety of URL handler mappings and we can use multiple URL Handler mappings in the same application.

Spring allows us to prioritize the URL Handler mappings. Every handler mapping class implements Ordered interface. You can set priority using order property and assign numeric values. Lower the value, higher the priority.

DispatcherServlet will consult each one of them in the order according to their priority set by order property. If a HandlerMapping does not return an appropriate HandlerExecutionChain (that is, it returns null), the next available HandlerMapping will be consulted. If no appropriate result is found after inspecting all HandlerMappings an exception will be thrown.

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
	    <property name="order" value="1" />
	</bean>
	
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/helloworld.html">helloWorldController</prop>
            </props>
        </property>
        <property name="order" value="0" />
    </bean>

Following example demonstrates how to configure multiple handler mappings and set priority.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>2.5.6</spring.version>
</properties>
 
<dependencies>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
</dependencies>

1. Controller

Now create HelloWorldController in com.kruders.controller package and write the following code.

HelloWorldController.java

package com.kruders.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("helloworld");
		model.addObject("message", "Hello World!!!");
		return model;
	}
}

Also create AddNumberController in com.kruders.controller package and write the following code.

AddNumberController.java

package com.kruders.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class AddNumberController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("addition");
		Integer number = 2 + 3;
		model.addObject("result", number.toString());
		return model;
	}
}

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file

${message}

Create another file addition.jsp

Now write the following code in addition.jsp file

Result : ${result}

3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
	    <property name="order" value="1" />
	</bean>
	
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/helloworld.html">helloWorldController</prop>
            </props>
        </property>
        <property name="order" value="0" />
    </bean>
    
    <bean id="helloWorldController" class="com.kruders.controller.HelloWorldController" />	
	
	<bean class="com.kruders.controller.AddNumberController" />

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5. Run

Now when you run the project, following screen will be displayed as in Figure 74.1

Figure 74.1 Figure 74.1

Now type http://localhost:8080/MultipleHandlerMapping/addnumbers.html, following screen will be displayed as in Figure 74.2

Figure 74.2 Figure 74.2

The folder structure of the example is shown below in Figure 74.3

Figure 74.3 Figure 74.3

You can download the source code of this example here.


, ,

2 Responses to Configure Multiple Handler Mappings in Spring MVC

  1. richardnixon December 10, 2012 at 8:32 pm #

    Just shows that there is a priority field, but doesn’t explain what the priorities do with regards to the mapped URLs. The version of Eclipse I have behaves much differently than the version they feature here, and there’s no mention of the version of Eclipse. I have not been able to get anything working.

    It would be more helpful if the explanation was fleshed out, a bit, and a zip or tar.gz file was provided with working code and configs.

    Thank you!

    • admin December 12, 2012 at 4:12 pm #

      @ richardnixon

      Source Code is available. You can download…

Leave a Reply