Handler mappings

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:

  1. http://servername:portnumber/ApplicationContext/welcome.htm
  2. http://servername:portnumber/ApplicationContext/listBooks.htm
  3. http://servername:portnumber/ApplicationContext/displayBookContent.htm

The controllers which will perform the business logic to fulfil the request made to the above pages are:

  1. net.codejava.frameorks.spring.mvc.controller.WelcomeController
  2. net.codejava.frameorks.spring.mvc.controller.ListBooksController
  3. 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.




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


   
      
         welcomeController
         listBooksController
         displayBookTOCController
      
   




The key of the element is the URL pattern of the incoming request. The value of the 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.

你可能感兴趣的:(Handler mappings)