Example Spring MVC Config handle mapping with ControllerClassNameHandlerMapping

In Spring MVC, ControllerClassNameHandlerMapping use convention to map requested URL to Controller (convention over configuration). It takes the Class name, remove the ‘Controller’ suffix if exists and return the remaining text, lower-cased and with a leading “/”.

1.Before and After

By default, Spring MVC is using the BeanNameUrlHandlerMapping handler mapping.

 

<beans ...>

 

  <bean name="/welcome.htm"

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

      

  <bean name="/helloGuest.htm"

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

 

</beans>

 

To enable the ControllerClassNameHandlerMapping, declared it in the bean configuration file, and now the controller’s bean’s name is no longer required.

 

<beans ...>

 

  <bean

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

      

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

      

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

 

</beans>

 

Now, Spring MVC is mapping the requested URL by following conventions :

WelcomeController -> /welcome*

HelloGuestController -> /helloguest*

 

/welcome.htm –> WelcomeController.

/welcomeHome.htm –> WelcomeController.

/helloguest.htm –> HelloGuestController.

/helloguest12345.htm –> HelloGuestController.

/helloGuest.htm, failed to map /helloguest*, the “g” case is not match.

2. Case sensitive

To solve the case sensitive issue stated above, declared the “caseSensitive” property and set it to true.

 

<beans ...>

 

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

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

  </bean>

      

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

      

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

 

</beans>

 

Now, Spring MVC is mapping the requested URL by the following conventions :

WelcomeController -> /welcome*

HelloGuestController -> /helloGuest*

/helloGuest.htm –> HelloGuestController.

/helloguest.htm, failed to map “/helloGuest*”, the “G” case is not match.

3. pathPrefix

Additionally, you can specify a prefix to maps the requested URL, declared a “pathPrefix” property.

 

<beans ...>

 

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

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

        <property name="pathPrefix" value="/customer" />

  </bean>

      

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

      

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

 

</beans>

 

Now, Spring MVC is mapping the requested URL by the following conventions :

WelcomeController -> /customer/welcome*

HelloGuestController -> /customer/helloGuest*

/customer/welcome.htm –> WelcomeController.

/customer/helloGuest.htm –> HelloGuestController.

/welcome.htm, failed.

/helloGuest.htm, failed.

Example detail

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 ControllerClassNameHandlerMapping</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. File 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.mvc.support.ControllerClassNameHandlerMapping">

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

              <!--<property name="pathPrefix" value="/customer" />-->

       </bean>

      

       <bean 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. File 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/welcome.htm

 

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

 

 

9. Link full code

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