深圳房地产评估价查询:spring学习笔记之handler mapping源码解读

来源:百度文库 编辑:中财网 时间:2024/05/11 05:06:44

spring学习笔记之handler mapping源码解读

spring学习笔记之handler mapping源码解读


   用 handler mapping 可以映射请求到合适的 handler ,比如用 SimpleUrlHandlerMapping 或者 BeanNameUrlHandlerMapping 。首先我们来看 handler mapping 这个概念。

   最基本的 HandlerMapping 提供的功能是 HandlerExecutionchain 的传递,必须包括请求匹配的处理器,也可能包括一系列应用于这个请求的的拦截器。当请求发生时, DispatcherServlet 将请求递交给 hangler mapping, 让其检测请求并提出一个合适的 HandlerExecutionChain.DispatcherServlet 将执行该处理器和拦截器。

   Handler mappings 对拦截器的配置是可选的,包括前,后执行或者前后都执行,功能很强大。很多已经支持的功能可以在 HandlerMappings 配置中定制。选择一个处理器的 handler mapping 定制不仅基于请求的 URL ,而且基于请求相关的 session 的状态。

所有 handler mapping 类都要继承 AbstractHandlerMapping 。下面是共有的属性。

Interceptors: 拦截器

defaultHandler: 默认的处理器,当没有匹配的处理器时使用。

Order: 基于 order 属性的值。

   BeanNameUrlHandlerMapping 简单而强大,根据定义在 web 应用环境的 bean 的名字映射 http 请求。比如说我们要让一名用户增加一帐号,我们已经提供了合适的表单控制器, jsp 或者 velocity 视图渲染表单。

如果使用 BeanNameUrlHandlerMapping, 请求 http://samples.com/editaccoutn.form. 配置如下:


view plaincopy to clipboardprint?

  1.    
  2.   
  •   
  •   
  •   
  •   
  •   
  •    

  • 所有请求的 url / editaccount.form 都会被 SimpleFormController 处理。当然在 web.xml 也要作如下的配置。

    view plaincopy to clipboardprint?

    1.   
    2.   
  • sample  
  • org.springframework.web.servlet.DispatcherServlet  
  • 1  
  •   
  •   
  •   
  • sample  
  • *.form  
  •   
  •    

  • 需要注意的是,你想用 BeanNameUrlHandlerMapping 是,并不需要定义。如果没有定义处理类,则 DispatcherServlet 默认创建它。

    SimpleUrlHandlerMapping 功能更强大。下面是例子

    view plaincopy to clipboardprint?

    1.   
    2.   
  • sample  
  • org.springframework.web.servlet.DispatcherServlet  
  • 1  
  •   
  •   
  •   
  • sample  
  • *.form  
  •   
  •   
  •   
  • sample  
  • *.html  
  •   
  •    

  • 上面例子表示以 .form 或者 .html 结尾的请求都会被下面的处理器处理。

    view plaincopy to clipboardprint?

    1.   
    2.   
  •   
  •   
  •   
  • /*/account.form=editAccountFormController  
  • /*/editaccount.form=editAccountFormController  
  • /ex/view*.html=helpController  
  • /**/help.html=helpController  
  •   
  •   
  •    

  • view plaincopy to clipboardprint?

    1.  
    2. class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>  
  • class="org.springframework.web.servlet.mvc.SimpleFormController">  
  •   
  •   
  •   
  •   
  •   
  •    

  • 上面配置的意思很容易看明白的。


    我们也可以用 HandlerInterceptor 拦截请求。拦截器必须实现 HandlerInterceptor 接口。这个接口定义了三个方法。见代码:

    view plaincopy to clipboardprint?

    1. boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
    2. throws Exception;  
    3. void postHandle(  
    4. HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  
    5. throws Exception;  
    6. void afterCompletion(  
    7. HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  
    8. throws Exception;   

    上面三个方面分别负责预处理,后置处理和请求完成后的处理。下面是例子:

    view plaincopy to clipboardprint?

    1.   
  • class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  •   
  •   
  •   
  •   
  •   
  •   
  •   
  • /*.form=editAccountFormController  
  • /*.view=editAccountFormController  
  •   
  •   
  •   
  • class="samples.TimeBasedAccessInterceptor">  
  •   
  •   
  •   
  •    



  • view plaincopy to clipboardprint?

    1. package samples;  
    2. public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {  
    3. private int openingTime;  
    4. private int closingTime;  
    5. public void setOpeningTime(int openingTime) {  
    6. this.openingTime = openingTime;  
  • }  
  • public void setClosingTime(int closingTime) {  
  • this.closingTime = closingTime;  
  • }  
  • public boolean preHandle(  
  • HttpServletRequest request,  
  • HttpServletResponse response,  
  • Object handler) throws Exception {  
  • Calendar cal = Calendar.getInstance();  
  • int hour = cal.get(HOUR_OF_DAY);  
  • if (openingTime <= hour < closingTime) {  
  • return true;  
  • } else {  
  • response.sendRedirect("http://host.com/outsideOfficeHours.html");  
  • return false;  
  • }  
  • }  
  • }  
  • 我们看到,拦截器类是继承了 HandlerInterceptorAdapter ,而 HandlerInterceptorAdapter 基本实现了 HandlerInterceptor 接口,接下来就是我们根据需要改写上面的三个某些方法了,而不需要全部实现。