[转]Handler Mapping in Spring MVC

Handler Mapping

The HandlerMapping strategy is used to map the HTTP client request to some handler controller and/or method. This is done based on the request URL and the HTTP method, but may also include the request parameters, request headers, or other custom factors.

 

HandlerMapping Strategy Interface

 

The interface specification for HandlerMapping is show below:

public interface HandlerMapping{
  HandlerExecutionChain getHandler(HttpServletRequest rqst)throwsException;
}

The single method getHandler(.) maps a request object to a execution chain object of type HandlerExecutionChain. The execution chain object wraps the handler (controller or method). It may also contain a set of interceptor objects of type HandlerInterceptor. Each interceptor may veto the execution of the handling request.

 

Provided HandlerMapping Strategies

 

Spring MVC support several strategies to map HTTP request to controllers, including: annotation based, name conventions, and explicit mappings. The classDefaultAnnotationHandlerMapping provides the default annotation based mapping strategy. The class BeanNameUrlHandlerMapping is another default strategy that maps URL based on beans names of controllers. The strategy ControllerClassNameHandlerMapping is used to enable class name convention mapping.

To add or replace a HandlerMapping strategy, one or more beans of this type should be declared. By default, Spring MVC install the strategies DefaultAnnotationHandlerMapping andBeanNameUrlHandlerMapping. If a HandlerMapping is declared explictily in the configuration the default handler mapping no longer are installed, unless <mvc:annotation-driven> is specified.

 

DefaultAnnotationHandlerMapping

 

The DefaultAnnotationHandlerMapping finds controller class annotated with handler methods @RequestMapping to establish the mapping. The value attribute of the annotation specifies the URL to map to the handler method. A DefaultAnnotationHandlerMapping is installed automatically by Spring MVC, provided that no other HandlerMapping is explicitly specified in the configuration file of the application. If other HandlerMapping are specified it can still be installed by default with <mvc:annotation-driven />.

With the following configuration:

<mvc:annotation-driven/>

The following controller will be mapped by DefaultAnnotationHandlerMapping.

@Controller
publicclassHomeController{ 
  @RequestMapping("/")
  voidpublic show(){
  }
}

An alternative and more explicty configuration is:

<beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<beanclass="myapp.web.HomeController"/>

 

BeanNameUrlHandlerMapping

 

The BeanNameUrlHandlerMapping maps URLs to handlers based on the bean name of controllers. The bean name of the controller should start with a leading '/'. ABeanNameUrlHandlerMapping is installed automatically by Spring MVC, provided that no other HandlerMapping is explicitly specified in the configuration file of the application. If otherHandlerMapping are specified it can still be installed by default with <mvc:annotation-driven />.

<mvc:annotation-driven/>

<beanname="/welcome.htm"class="myapp.web.HomeController"/>

Or more explicitly:

<beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
<beanname="/welcome.htm"class="myapp.web.HomeController"/>

BeanNameUrlHandlerMapping supports Ant-style patterns in the bean name. Below, are some examples:

<beanname="/show*"class="myapp.web.ShowController"/>
<beanname="/account**"class="myapp.web.AccountController"/>
<beanname="/product/**"class="myapp.web.ProductController"/>

The first controller maps to URLs such as: /show/showAccount/showProduct, but not /show/Account. Thw second controller maps to URLs such as: /account/account/show, and/account/address/show. The third controller maps to /product/, and /product/show, but not /product.

Table below summaties these examples of mathing and non-matching URLs for the controller defined above (assuming BeanNameUrlHandlerMapping is configured).

Example Controller

Pattern in Bean Name Matches No Match
ShowController /show* /show/showAccount/showProduct /show/Account
AccountController /account** /account/account/show/accounts /Account
ProductController /product/** /product//product/show /product
Pattern Matching with BeanNameUrlHandlerMapping

 

ControllerClassNameHandlerMapping

 

The ControllerClassNameHandlerMapping maps URL to handlers based on the class name of controllers. Controler classes should be suffixed with the word Controller, such as inHomeController or AccountController. The set of controller beans considered are those annotated with @Controller or that derive from class Controller.

The mapping convention is that the mapped URL is derived from the uncapitalized class name removed from the word Controller. More preciselly, HomeController is mapped to /home*.

ControllerClassNameHandlerMapping needs to be configured explicitly since it is not installed by default by SpringMVC.

<beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

The controller class below will be picked up by ControllerClassNameHandlerMapping.

@Controller
publicclassAccountController{ 
  @RequestMapping("/")
  voidpublic show(@RequestParam("id")Long id){
    ...
  }

  @RequestMapping
  voidpublic list(){
    ...
  }

  
}

The handler method show() is mapped to URL such as /account/?id=123, while method list() is mapped to URL /account/list.

Looking to the console output of Spring MVC log is a useful way to check the exact URLs that are setup for a given configuration:

INFO : org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping-Mapped URL path [/account] onto handler 'accountController'
INFO : org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping-Mapped URL path [/account/*] onto handler 'accountController'

The ControllerClassNameHandlerMapping can be used with controller with multiple handler methods, provided the the installed HandlerAdapter know how to deal with this.

 

SimpleUrlHandlerMapping

 

The SimpleUrlHandlerMapping provides a way to explicitly map URLs to controller beans by direct configuration. This is done by setting property mappings.

<beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <propertyname="mappings">
    <props>
      <propkey="/showAccount">AccountController</prop>
      <propkey="/listAccounts">AccountController</prop>
      <propkey="/welcome*">WelcomeController</prop>
    </props>
  </property>
</bean>

The configuration above maps /showAccount and /listAccounts to AccountController, and /welcome* to WelcomeController.

Because the Spring bean container (application context) has a PropertyEditor for the type java.util.Propery, the following alternative configuration can also be used:

<beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <propertyname="mappings">
        <value>
    /showAccount = AccountController
    /listAccounts = AccountController
    /welcome* = WelcomeController
    </value>
  </property>
</bean>

 

References and External Resources

 

from : http://www.jpalace.org/docs/tutorials/spring/mvc_8.html

猜你喜欢

转载自dean-liu.iteye.com/blog/1959820