Example Spring MVC Configure the handler mapping priority

Often times, you may mix use of multiple handler mappings strategy in Spring MVC development.

For example, use ControllerClassNameHandlerMapping to map all the convention handler mappings, and SimpleUrlHandlerMapping to map other special handler mappings explicitly.

<beans ...>

 

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

      <property name="mappings">

              <value>

                     /index.htm=welcomeController

                     /welcome.htm=welcomeController

                     /main.htm=welcomeController

                     /home.htm=welcomeController

              </value>

      </property>

      <property name="order" value="0" />

   </bean>

      

   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >

      <property name="caseSensitive" value="true" />

      <property name="order" value="1" />

   </bean>   

      

   <bean id="welcomeController"

      class="com.mkyong.common.controller.WelcomeController" />

             

   <bean class="com.mkyong.common.controller.HelloGuestController" />

             

</beans>

In above case, it’s important to specify the handler mapping priority, so that it won’t cause the conflict. You can set the priority via the “order” property, where the lower order value has the higher priority.

Example:

1. Project structure

 

2. File pom.xml

 

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codewr.common</groupId>

    <artifactId>SpringMVC</artifactId>

    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>SpringMVC Maven HandlerMapping-Priority</name>

    <url>http://maven.apache.org</url>

    <dependencies>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

      

        <!-- Spring framework -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring</artifactId>

            <version>2.5.6</version>

        </dependency>

 

        <!-- Spring MVC framework -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>2.5.6</version>

        </dependency>

      

        <!-- JSTL -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

            <version>1.1.2</version>

        </dependency>

      

        <dependency>

            <groupId>taglibs</groupId>

            <artifactId>standard</artifactId>

            <version>1.1.2</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>javax.servlet-api</artifactId>

            <version>3.1.0</version>

            <type>jar</type>

        </dependency>

    </dependencies>

    <build>

        <finalName>SpringMVC</finalName>

    </build>

</project>

 

3. HelloGuestController.java

 

package com.codewr.common.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 HelloGuestController extends AbstractController {

 

    @Override

    protected ModelAndView handleRequestInternal(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

 

        ModelAndView model = new ModelAndView("HelloWorldPage");

        model.addObject("msg", "HelloGuestController");

 

        return model;

    }

 

}

 

4. WelcomeController.java

 

package com.codewr.common.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 WelcomeController extends AbstractController {

 

    @Override

    protected ModelAndView handleRequestInternal(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

 

        ModelAndView model = new ModelAndView("HelloWorldPage");

        model.addObject("msg", "WelcomeController");

 

        return model;

    }

 

}

 

5. HelloWorldPage.jsp

 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

    <body>

        <h1>Spring MVC Hello World Example</h1>

 

        <h2>${msg}</h2>

 

    </body>

</html>

 

6. mvc-dispatcher-servlet.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="mappings">

            <value>

                /index.htm=welcomeController

                /welcome.htm=welcomeController

                /main.htm=welcomeController

                /home.htm=welcomeController

            </value>

        </property>

        <property name="order" value="0" />

    </bean>

      

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >

        <property name="caseSensitive" value="true" />

        <property name="order" value="1" />

    </bean>  

      

    <bean id="welcomeController"

              class="com.codewr.common.controller.WelcomeController" />

             

    <bean class="com.codewr.common.controller.HelloGuestController" />

      

    <bean id="viewResolver"

              class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

        <property name="prefix">

            <value>/WEB-INF/pages/</value>

        </property>

        <property name="suffix">

            <value>.jsp</value>

        </property>

    </bean>

 

</beans>

 

7. web.xml

 

<web-app id="WebApp_ID" version="2.4"

         xmlns="http://java.sun.com/xml/ns/j2ee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

    <display-name>Spring Web MVC Application</display-name>

 

    <servlet>

        <servlet-name>mvc-dispatcher</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>mvc-dispatcher</servlet-name>

        <url-pattern>*.htm</url-pattern>

    </servlet-mapping>

 

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>

    </context-param>

 

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

</web-app>

 

8. Run web

http://localhost:8080/SpringMVC/index.htm

 

http://localhost:8080/SpringMVC/welcome.htm

http://localhost:8080/SpringMVC/main.htm

http://localhost:8080/SpringMVC/home.htm

http://localhost:8080/SpringMVC/helloGuest.htm

 

9. Link full code:

https://github.com/sieurobo196/Spring-MVC/tree/master/SpringMVC-HandlerMapping-Priority