Example Spring MVC Config handle mapping with SimpleUrlHandlerMapping

In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly.

The SimpleUrlHandlerMapping can be declared in two ways.

1. Method 1 – prop key

The property keys are the URL patterns while the property values are the handler IDs or names.

 

<beans ...>

 

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

          <property name="mappings">

              <props>

                 <prop key="/welcome.htm">welcomeController</prop>

                 <prop key="/*/welcome.htm">welcomeController</prop>

                 <prop key="/helloGuest.htm">helloGuestController</prop>

               </props>

          </property>

       </bean>

      

       <bean id="welcomeController"

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

             

       <bean id="helloGuestController"

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

             

</beans>

 

2. Method 1 – value

The left side are the URL patterns while the right side are the handler IDs or names, separate by a equal symbol “=”.

 

<beans ...>

      

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

          <property name="mappings">

              <value>

                 /welcome.htm=welcomeController

                 /*/welcome.htm=welcomeController

                 /helloGuest.htm=helloGuestController

              </value>

          </property>

       </bean>

      

       <bean id="welcomeController"

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

             

       <bean id="helloGuestController"

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

             

</beans>

 

Example: config by method Value

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 SimpleUrlHandlerMapping</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>

                /welcome.htm=welcomeController

                /*/welcome.htm=welcomeController

                /helloGuest.htm=helloGuestController

            </value>

        </property>

    </bean>

      

    <bean id="welcomeController"

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

             

    <bean id="helloGuestController"

          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

/welcome.htm –> welcomeController.

/{anything}/welcome.htm –> welcomeController.

/helloGuest.htm –> helloGuestController.

 

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

 

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

 

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

 

9. Link full code:

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