BeanNameUrlHandlerMapping is the default handler mapping class, which maps the URL requests to the names of the beans. Additionally, the mapping is support the Ant style regex pattern match as well, see the AntPathMatcher javadoc for details. For example,

<beans ...>
 
   <bean 
     class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
   <bean name="/welcome.htm" 
            class="com.mkyong.common.controller.WelcomeController" />
 
   <bean name="/streetName.htm" 
            class="com.mkyong.common.controller.StreetNameController" />
 
   <bean name="/process*.htm" 
            class="com.mkyong.common.controller.ProcessController" />
 
</beans>

In above example, If URL pattern

  1. /welcome.htm is requested, DispatcherServlet will forward the request to the “WelcomeController“.
  2. /streetName.htm is requested, DispatcherServlet will forward the request to the “StreetNameController“.
  3. /processCreditCard.htm or /process{any thing}.htm is requested, DispatcherServlet will forward the request to the “ProcessController“.
Note
Actually, you don’t need to define the BeanNameUrlHandlerMapping in the web.xml, by default, if no handler mapping can be found, the DispatcherServlet will creates a BeanNameUrlHandlerMapping automatically. So, the above web.xml file is equivalence to the following :

web.xml

<beans ...>
 
   <bean name="/welcome.htm" 
            class="com.mkyong.common.controller.WelcomeController" />
 
   <bean name="/streetName.htm" 
            class="com.mkyong.common.controller.StreetNameController" />
 
   <bean name="/process*.htm" 
            class="com.mkyong.common.controller.ProcessController" />
 
</beans>

P.S Declared the BeanNameUrlHandlerMapping is optional.

Reference

  1. BeanNameUrlHandlerMapping javadoc
  2. AntPathMatcher javadoc
This article was posted in Spring MVC category.