`
dean_liu
  • 浏览: 75308 次
  • 性别: 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 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-ext-handler-mapping.rar_ext_spring ext_spring mvc

    扩展spring地址方法映射,让spring MVC开发更简便

    spring mvc 3.2 参考文档

    Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...

    springmvc demo

    应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)...

    Web框架编程-SpringMVC框架编程

    Spring MVC(Model-View-Controller)是一个基于Java的MVC(模型-视图-控制器)框架,用于构建Web应用程序。它是Spring Framework的一部分,提供了一种简单、灵活且可扩展的方式来开发Web应用程序。 下面是Spring ...

    SPRING入门

    应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)...

    看透springMvc源代码分析与实践

    8.2.2 创建Spring MVC的xml配置文件85 8.2.3 创建Controller和view86 8.3 关联spring源代码87 8.4 小结89 第9章 创建Spring MVC之器90 9.1 整体结构介绍90 9.2 HttpServletBean93 9.3 FrameworkServlet95 ...

    Spring-MVC技术体系介绍(二)

    控制器(controller)、验证器(validator)、 命令对象(command object)、表单对象(form object)、模型对象(model object)、 Servlet分发器(DispatcherServlet)、 处理器映射(handler mapping)、视图解析...

    开源框架 Spring Gossip

    第一个 Spring MVC 程式 WebApplicationContext Handler Mapping Handler Interceptor Controller 继承架构 ModelAndView View Resolver Exception Resolver 使用 Controller ...

    SpringMvc源码

    应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)...

    spring security 参考手册中文版

    5.1.3使用Spring MVC的AbstractSecurityWebApplicationInitializer 32 5.2 HttpSecurity 32 5.3 Java配置和表单登录 34 5.4授权请求 35 5.5处理注销 36 5.5.1 LogoutHandler 37 5.5.2 LogoutSuccessHandler 37 5.5.3...

    跟我学SpringMVC

    应用控制器其 实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动 作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器 13.5.2. 视图解析链 ...

    Spring 2.0 开发参考手册

    13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器 13.5.2. 视图...

    Spring中文帮助文档

    13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器(ViewResolver) ...

    spring chm文档

    13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器 13.5.2. 视图...

    Spring API

    13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器(ViewResolver) ...

    spring SimpleUrlHandlerMapping工程使用源码

    1.使用了spring SimpleUrlHandlerMapping实现handler Mapping. 2.使用了HandlerIntercepterAdaptor实现Controller的拦截处理 使用方法: 1.导入工程 2.键入...

    springmybatis

    mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;!-- 采用单数据源事务控制方式,通过注解来定义事务--&gt; class="org.springframework.jdbc....

Global site tag (gtag.js) - Google Analytics