Spring MVC – BeanNameUrlHandlerMapping example
Written on July 29, 2010 at 2:49 pm by
mkyong
Download it – SpringMVC-BeanNameUrlHandlerMapping-Example.zip
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
- /welcome.htm is requested, DispatcherServlet will forward the request to the “WelcomeController“.
- /streetName.htm is requested, DispatcherServlet will forward the request to the “StreetNameController“.
- /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 :
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
This article was posted in Spring MVC category.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
[...] DispatcherServlet will creates a BeanNameUrlHandlerMapping automatically. See this article – BeanNameUrlHandlerMapping example for [...]