`
dean_liu
  • 浏览: 76210 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

[转]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

分享到:
评论

相关推荐

    spring-ext-handler-mapping.rar_ext_spring ext_spring mvc

    本资源"spring-ext-handler-mapping"着重于扩展Spring的地址映射功能,使得开发者在使用Spring MVC进行Web开发时能够更加灵活和高效。 1. **扩展Spring HandlerMapping** Spring MVC默认提供了一些内置的...

    spring MVC配置详解

    DispatcherServlet 是 Spring MVC 框架的核心组件,它负责转发每一个 Request 请求给相应的 Handler,Handler 处理以后再返回相应的视图(View)和模型(Model)。DispatcherServlet 是继承自 HttpServlet 的,既然 ...

    Spring mvc 教程

    #### 处理器映射 (Handler Mapping) - **使用 HandlerInterceptor 拦截请求**:这些拦截器可以在请求到达控制器之前或之后执行某些操作,比如权限验证或记录日志。 #### 视图解析 (View Resolution) - **使用 ...

    Spring MVC源码深度剖析开源架构源码2021.pdf

    Spring MVC为不同类型的Handler提供了不同的HandlerAdapter,比如HttpRequestHandlerAdapter、SimpleControllerHandlerAdapter以及用于处理@RequestMapping注解的RequestMappingHandlerAdapter。 当Handler处理请求...

    Spring MVC 入门实例

    11 &lt;bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; 12 13 14 &lt;prop key="/hello.do"&gt;helloController 15 16 17 18 19 20 &lt;!-- 21 22 --&gt; ...

    spring mvc 附件上传代码

    - `&lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;`定义了请求映射器。 - `&lt;property name="mappings"&gt;`通过`&lt;props&gt;`标签配置了URL到控制器的映射关系:`/...

    Spring MVC

    Spring MVC提供多种Handler Mapping实现,如BeanNameUrlHandlerMapping,它将URL与Controller的类名直接对应,适合小型应用。在实际开发中,更常见的是使用SimpleUrlHandlerMapping,它可以根据URL模式匹配到特定的...

    SPRING MVC配置过程

    SPRING MVC 是一个基于 DispatcherServlet 的 MVC 框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet 负责转发每一个 Request 请求给相应的 Handler,Handler 处理以后再返回相应的视图(View)和...

    spring mvc的配置文件详解

    &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/&gt; ``` 这里创建了一个 BeanNameUrlHandlerMapping 的实例,Spring 会自动将其注册为 HandlerMapping。 ##### ...

    Spring MVC tutorial

    1. **Spring MVC配置**:在`web.xml`中配置DispatcherServlet,设置servlet-mapping和servlet-class。 2. **Bean定义**:在Spring的配置文件中,定义Controller、Service和DAO层的bean。 3. **MVC配置**:使用`...

    Spring MVC 框架搭建及详解

    2. **web.xml配置**: 这是Spring MVC的入口点,通过配置`&lt;servlet&gt;`和`&lt;servlet-mapping&gt;`元素来指定DispatcherServlet,它是Spring MVC的核心,负责调度请求。`&lt;servlet-name&gt;`定义了Servlet的名称,`...

    Spring MVC入门 XML版本+注释版本

    Spring MVC是Spring框架的一部分,主要用于构建Web应用程序的模型-视图-控制器(MVC)架构。这个入门教程将涵盖XML配置和注解配置两种方式,帮助初学者理解如何在Spring MVC中搭建基本的"Hello, World!"应用。 首先...

    spring mvc入门教程

    4. **映射处理器(Handler Mapping)**:映射处理器负责将请求URL映射到具体的处理方法。 5. **视图解析器(View Resolver)**:视图解析器负责解析返回的视图名称到具体的视图实现。 6. **注解配置**:Spring MVC...

    spring MVC Helloworld

    **Spring MVC HelloWorld 实例详解** 在Java Web开发中,Spring MVC框架被广泛使用,它为构建基于模型-视图-控制器(MVC)模式的Web应用程序提供了强大的支持。本篇文章将详细讲解如何在MyEclipse2013环境中创建一...

    Spring MVC 框架学习总结

    3. **Handler Mapping** - 映射处理器,将请求映射到相应的 Controller 方法。 - 可配置多种映射策略,如基于注解的 URL 映射,XML 配置等。 4. **Controller** - 处理用户请求的类,通常使用注解@Controller来...

    spring mvc学习视频相关资料

    Spring MVC 的核心是 DispatcherServlet,它作为前端控制器,接收所有请求,然后根据请求信息找到合适的处理器(Handler)。 4. **处理器映射器(Handler Mapping)** 映射用户请求到相应的 Controller 方法,如 ...

    基于xml配置的spring mvc Helloworld实例

    在IT行业中,Spring MVC是一个广泛使用的Java Web框架,它提供了模型-视图-控制器(MVC)架构,便于开发高效、可维护的Web应用程序。在这个基于XML配置的Spring MVC HelloWorld实例中,我们将深入理解如何设置并运行...

    Spring MVC 配置请求的默认处理器.rar

    在Spring MVC框架中,配置请求的默认处理器是构建Web应用程序的关键步骤。这个过程涉及到设置DispatcherServlet,它作为Spring MVC的核心组件,负责调度HTTP请求到相应的控制器。以下将详细阐述Spring MVC配置请求的...

    spring mvc学习笔记

    ### Spring MVC 学习笔记 #### 一、Spring MVC 核心组件与工作流程 **Spring MVC** 是 **Spring Framework** 的一个重要模块,主要用于构建基于 Web 的应用程序。它提供了简化 Web 开发的一系列功能,包括但不限于...

    spring mvc helloworld

    这包括视图解析器(View Resolver)、模型-视图映射(Model-View Mappings)以及处理器映射器(Handler Mapping)等。一个简单的配置示例如下: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans...

Global site tag (gtag.js) - Google Analytics