Example Spring MVC config handle mapping with BeanNameUrlHandlerMapping

In Spring MVC, BeanNameUrlHandlerMapping is the default handler mapping mechanism, which maps URL requests to the name of the beans.

Example:

 

<beans ...>

 

   <bean

       class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

      

   <bean name="/welcome.htm"

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

   <bean name="/process*.htm"

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

 

</beans>

In above example, If URI pattern

- /welcome.htm is requested, DispatcherServlet will forward the request to the “WelcomeController“.

- /processCreditCard.htm or /process{any thing}.htm is requested, DispatcherServlet will forward the request to the “ProcessController“.

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 BeanNameUrlHandlerMapping </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. Class HelloWorldController.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;

 

/**

 *

 * @author codewr

 */

public class HelloWorldController extends AbstractController {

 

    @Override

    protected ModelAndView handleRequestInternal(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

 

        ModelAndView model = new ModelAndView("HelloWorldPage");

        model.addObject("msg", "hello world");

 

        return model;

    }

 

}

 

4. Class ProcessController.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;

 

/**

 *

 * @author codewr

 */

public class ProcessController extends AbstractController {

 

    @Override

    protected ModelAndView handleRequestInternal(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

 

        ModelAndView model = new ModelAndView("ProcessPage");

        model.addObject("msg", "page process");

 

        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. ProcessPage.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>

 

7. 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.handler.BeanNameUrlHandlerMapping"/>

      

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

 

       <bean name="/welcome.htm" class="com.codewr.common.controller.HelloWorldController" />

      

       <bean name="/process*.htm" class="com.codewr.common.controller.ProcessController" />

 

</beans>

 

8. 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>

 

9. Run web

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

 

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


10. Link full code

https://github.com/sieurobo196/Spring-MVC/tree/master/SpringMVC-BeanName-URL-Handler-Mapping