Handler mappings
标签(空格分隔): SpringMVC
转自 CodeJava
As the name specifies, the handler mapping maps the request with the corresponding request handler (in fact handler execution chain). When a request comes to Spring’s dispatcher servlet, it hands over the request to the handler mapping. Handler mapping then inspects the request and identifies the appropriate handler execution chain and delivers it to dispatcher servlet. The handler execution chain contains handler that matches the incoming request and optionally contains the list of interceptors that are applied for the request. Dispatcher servlet then executes the handlers and any associated handler interceptor.
There are number of implementation of hander mapping provided by Spring’s MVC module. Some of these are described below. All the handler mappings classes implement the interface:
org.springframework.web.servlet.HandlerMapping
BeanNameUrlHandlerMapping
This implementation of handler mapping matches the URL of the incoming request with the name of the controller beans. The matching bean is then used as the controller for the request. This is the default handler mapping used by the Spring’s MVC module i.e. in case the dispatcher servlet does not find any handler mapping bean defined in Spring’s application context then the dispatcher servlet uses BeanNameUrlHandlerMapping.
Let us assume that we have three web pages in our application. The URL of the pages are:
- http://servername:portnumber/ApplicationContext/welcome.htm
- http://servername:portnumber/ApplicationContext/listBooks.htm
- http://servername:portnumber/ApplicationContext/displayBookContent.htm
The controllers which will perform the business logic to fulfil the request made to the above pages are:
- net.codejava.frameorks.spring.mvc.controller.WelcomeController
- net.codejava.frameorks.spring.mvc.controller.ListBooksController
- net.codejava.frameorks.spring.mvc.controller.DisplayBookTOCController
Thus we need to define the controllers in Spring’s application context file such that the name of the controller matches the URL of the request. The controller beans in XML configuration file will look as below.
<bean
name="/welcome.htm"
class="net.codejava.frameorks.spring.mvc.controller.WelcomeController" />
<bean
name="/listBooks.htm"
class="net.codejava.frameorks.spring.mvc.controller.ListBooksController"/>
<bean
name="/displayBookTOC.htm"
class="net.codejava.frameorks.spring.mvc.controller.DisplayBookTOCController"/>
Note that we need not define the BeanNameUrlHandlerMapping in Spring’s application context file because this is the default one being used.
SimpleUrlHandlerMapping
The BeanNameUrlHandlerMapping puts a restriction on the name of the controller beans that they should match the URL of the incoming request. SimpleUrlHandlerMapping removes this restriction and maps the controller beans to request URL using a property “mappings”.
<bean
id="myHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.htm">welcomeController</prop>
<prop key="/listBooks.htm">listBooksController</prop>
<prop key="/displayBookTOC.htm">displayBookTOCController</prop>
</props>
</property>
</bean>
<bean name="welcomeController"
class="net.codejava.frameorks.spring.mvc.controller.WelcomeController"/>
<bean name="listBooksController"
class="net.codejava.frameorks.spring.mvc.controller.ListBooksController"/>
<bean name="displayBookTOCController"
class="net.codejava.frameorks.spring.mvc.controller.DisplayBookTOCController"/>
The key of the <prop> element is the URL pattern of the incoming request. The value of the <prop> element is the name of the controller bean which will perform the business logic to fulfil the request. SimpleUrlHandlerMapping is one of the most commonly used handler mapping.