加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[HandlerMapping]1 详解Spring MVC的处理器映射HandlerMapping和拦截器HandlerInterceptor

(2012-04-18 16:14:16)
标签:

spring

mvc

it

处理器映射

handlermapping

拦截器

interceptor

A、HandlerMapping(处理映射)

org.springframework.web.servlet.HandlerMapping

public interface HandlerMapping

Interface to be implemented by objects that define a mapping between requests and handler objects.

HandlerMapping接口的实现对象定义了web请求映射和处理器(Handler)之间的映射。

This class can be implemented by application developers, although this is not necessary, as BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping are included in the framework. The former is the default if no HandlerMapping bean is registered in the application context.

这个类可以由应用程序开发者来实施,虽然它不是必须的,你可以使用Spring内建的类(如:BeanNameUrlHandlerMapping、SimpleUrlHandlerMapping),若上下文中未定义HandlerMapping,那么BeanNameUrlHandlerMapping就是缺省配置。

HandlerMapping implementations can support mapped interceptors but do not have to. A handler will always be wrapped in a HandlerExecutionChain instance, optionally accompanied by some HandlerInterceptor instances. The DispatcherServlet will first call each HandlerInterceptor's preHandle method in the given order, finally invoking the handler itself if all preHandle methods have returned true.

HandlerMapping支持拦截器,但不是必须的。一个处理器(Handler)总是把请求传递到一个HandlerExecutionChain实例中,也可以选择包含一些HandlerInterceptor实例。DispatcherServlet首先按照给定的顺序执行每个HandlerInterceptor的preHandle方法,若所有的preHandle方法都返回true,则就执行处理器本身。

The ability to parameterize this mapping is a powerful and unusual capability of this MVC framework. For example, it is possible to write a custom mapping based on session state, cookie state or many other variables.

         参数化映射是非常有用的。例如,它可以自定义一个居于session状态,cookie状态或其它变量的映射。

Note: Implementations can implement the Ordered interface to be able to specify a sorting order and thus a priority for getting applied by DispatcherServlet. Non-Ordered instances get treated as lowest priority.

         注意,实现Ordered接口的实例可以指定一个排序的order,从而实现DispatcherServlet应用的优先级。没有指定order的实例将被当作最低优先级来处理。

 

A.1、AbstractHandlerMapping(基初抽象类)

org.springframework.web.servlet.handler.AbstractHandlerMapping

public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered

java.lang.Object
  
 
 
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 

 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.handler.AbstractHandlerMapping

Abstract base class for HandlerMapping implementations. Supports ordering, a default handler, and handler interceptors.

HandlerMapping实例的基础抽象类。支持顺序(order),默认处理器(dedault handler),拦截器(HandlerInterceptor)。

Note: This base class does not support exposure of the HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE. Support for this attribute is up to concrete subclasses, typically based on request URL mappings.

         注意,这个基类不支持HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE。它的子类有支持这个属性的,通常基于请求的URL映射。

Set方法

void  setOrder(int order)

Specify the order value for this HandlerMapping bean.

指定HandlerMapping bean的order值。Spring上下文Context中可用的HandlerMapping进行排序,然后选用第一个和请求request匹配的处理器。

void  setInterceptors(Object[] interceptors)

Set the interceptors to apply for all handlers mapped by this handler mapping.

设定拦截器interceptor列表。

void  setDefaultHandler(Object defaultHandler)

Set the default handler for this handler mapping.

设定默认处理器Controller。但没有合适的Controller匹配时,使用该处理器。

 

A.1.1、AbstractUrlHandlerMapping(URL抽象类)

org.springframework.web.servlet.handler.AbstractUrlHandlerMapping

public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping

java.lang.Object
  
 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
              
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping

Abstract base class for URL-mapped HandlerMapping implementations. Provides infrastructure for mapping handlers to URLs and configurable URL lookup. For information on the latter, see "alwaysUseFullPath" property.

是URL-mapped(根据url映射)的抽象基类。它为映射URL到处理程序和URL查询提供基础功能。

Supports direct matches, e.g. a registered "/test" matches "/test", and various Ant-style pattern matches, e.g. a registered "/t*" pattern matches both "/test" and "/team", "/test/*" matches all paths in the "/test" directory, "/test/**" matches all paths below "/test". For details, see the AntPathMatcher javadoc.

支持直接匹配(如:/test匹配/test),支持各种ant-style模式的匹配(如:/t*匹配/test和team等)。详细参阅AntPathMatcher文档。

Set方法

void  setAlwaysUseFullPath(boolean alwaysUseFullPath)

Set if URL lookup should always use the full path within the current servlet context.

设置是否使用完整路径。默认为false。若servlet的servlet-mapping中用/test/*,若设置为true时,url必须使用/test/view.html,若为false,则可以使用相对路径/view.html

void  setLazyInitHandlers(boolean lazyInitHandlers)

Set whether to lazily initialize handlers.

void  setPathMatcher(PathMatcher pathMatcher)

Set the PathMatcher implementation to use for matching URL paths against registered URL patterns.

void  setRootHandler(Object rootHandler)

Set the root handler for this handler mapping, that is, the handler to be registered for the root path ("/").

void  setUrlDecode(boolean urlDecode)

Set if context path and request URI should be URL-decoded.

默认为true。

void  setUrlPathHelper(UrlPathHelper urlPathHelper)

Set the UrlPathHelper to use for resolution of lookup paths.

 

A.1.1.1、SimpleUrlHandlerMapping(实现类)

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping

java.lang.Object
  
 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
              
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
                  
 
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

Implementation of the HandlerMapping interface to map from URLs to request handler beans. Supports both mapping to bean instances and mapping to bean names; the latter is required for non-singleton handlers.

HandlerMapping接口的一个实现,把URL映射到请求处理bean。支持映射到bean实例和bean名称。后者需要一个non-singleton处理器。

The "urlMap" property is suitable for populating the handler map with bean references, e.g. via the map element in XML bean definitions.

urlMap属性适合填入通过bean参考的处理映射。例如:在xml定义映射元素。

Mappings to bean names can be set via the "mappings" property, in a form accepted by the java.util.Properties class, like as follows:

/welcome.html=ticketController /show.html=ticketController

The syntax is PATH=HANDLER_BEAN_NAME. If the path doesn't begin with a slash, one is prepended.

映射到bean名可以通过mappings属性来设置,mappings属性是用Properties格式来设置的。例如:/welcome.html=ticketController。语法为PATH=HANDLER_BEAN_NAM

Set方法

void  setMappings(Properties mappings)

Map URL paths to handler bean names.

void  setUrlMap(Map urlMap)

Set a Map with URL paths as keys and handler beans (or handler bean names) as values.

SimpleUrlHandlerMapping配置范例

<!-- 配置方法一 对urlMap配置   -->

< bean id="SimpleUrlHM1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="urlMap">

<map>

<entry key="/user/login.do" value-ref="loginController" />

</map>

</property>

</bean>

 

<!-- 配置方法二 对mappings配置-->

<bean id="SimpleUrlHM2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/user/login.do">loginController</prop>

</props>

</property>

</bean>

 

<!-- 配置方法三 使用properties对mappings配置 -->

<bean id="SimpleUrlHM3" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<property name="location">

               <value>urlMap.properties</value>

<!-- 此时urlMap.properties文件应放在WebRoot目录下! -->

           </property>

       </bean>

    </property>

</bean>

 

A.1.1.2、AbstractDetectingUrlHandlerMapping

org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping

public abstract class AbstractDetectingUrlHandlerMapping extends AbstractUrlHandlerMapping

java.lang.Object
  
 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
           
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
              
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping

Abstract implementation of the HandlerMapping interface, detecting URL mappings for handler beans through introspection of all defined beans in the application context.

实现HandlerMapping接口的抽象类,通过检查所有在应用程序上下文中定义的bean来探测处理bean的url映射。

Set方法

void  setDetectHandlersInAncestorContexts(boolean detectHandlersInAncestorContexts)

Set whether to detect handler beans in ancestor ApplicationContexts.

 

A.1.1.2.1、BeanNameUrlHandlerMapping

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping

 org.springframework.context.support.ApplicationObjectSupport
   
 
org.springframework.web.context.support.WebApplicationObjectSupport
       
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
           
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
               
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
                   
 
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

Implementation of the HandlerMapping interface that map from URLs to beans with names that start with a slash ("/"), similar to how Struts maps URLs to action names.

HandlerMapping接口的实现,它通过bean的name来映射url,这个name以/开头。

This is the default implementation used by the DispatcherServlet, along with DefaultAnnotationHandlerMapping (on Java 5 and higher). Alternatively, SimpleUrlHandlerMapping allows for customizing a handler mapping declaratively.

它是DispatcherServlet默认实现。

The mapping is from URL to bean name. Thus an incoming URL "/foo" would map to a handler named "/foo", or to "/foo /foo2" in case of multiple mappings to a single handler. Note: In XML definitions, you'll need to use an alias name="/foo" in the bean definition, as the XML id may not contain slashes.

映射/foo,通过bean的name属性为/foo。name=”/foo /foo2”表示多个url映射到一个handler。

Supports direct matches (given "/test" -> registered "/test") and "*" matches (given "/test" -> registered "/t*").

支持直接匹配和*匹配。

BeanNameUrlHandlerMapping配置范例

把一个BeanNameUrlHandlerMapping映射到一个SimpleFormController

<bean id="BeanNameUrlHM1" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

</bean>

<bean name="/test.form" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

</bean>

 

A.1.1.2.2、AbstractControllerUrlHandlerMapping

org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping

public abstract class AbstractControllerUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping

org.springframework.context.support.ApplicationObjectSupport
  
 
org.springframework.web.context.support.WebApplicationObjectSupport
    
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
      
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
       
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
        
 
org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping

Base class for HandlerMapping implementations that derive URL paths according to conventions for specific controller types.

实现HandlerMapping的抽象基类,它为特定的控制器类型根据规则取得url路径。

Set方法

void  setExcludedClasses(Class[] excludedClasses)

Specify controller classes that should be excluded from this mapping.

void  setExcludedPackages(String[] excludedPackages)

Specify Java packages that should be excluded from this mapping.

void  setIncludeAnnotatedControllers(boolean includeAnnotatedControllers)

Set whether to activate or deactivate detection of annotated controllers.

 

A.1.1.2.2.1、ControllerBeanNameHandlerMapping

org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping

org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping
  
 
org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping

public class ControllerBeanNameHandlerMapping extends AbstractControllerUrlHandlerMapping

org.springframework.context.support.ApplicationObjectSupport
 
 
org.springframework.web.context.support.WebApplicationObjectSupport
  
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
   
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
    
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
    
 
org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping
      
 
org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping

Implementation of HandlerMapping that follows a simple convention for generating URL path mappings from the bean names of registered Controller beans as well as @Controller annotated beans.

HandlerMapping的实现,它根据一个简单的规则来生成一个url路径映射,这个映射来自一个注册的控制器的bean名称,以及它的@Controller注释。

This is similar to BeanNameUrlHandlerMapping but doesn't expect bean names to follow the URL convention: It turns plain bean names into URLs by prepending a slash and optionally applying a specified prefix and/or suffix. However, it only does so for well-known controller types, as listed above (analogous to ControllerClassNameHandlerMapping).

它和BeanNameUrlHandlerMapping相似,但它不期望遵循url规则的bean名称:它把普通的bean名称转化为url,以/开头,可以有前缀或后缀。

Set方法

void  setUrlPrefix(String urlPrefix)

Set an optional prefix to prepend to generated URL mappings.

void  setUrlSuffix(String urlSuffix)

Set an optional suffix to append to generated URL mappings.

 

A.1.1.2.2.2、ControllerClassNameHandlerMapping

org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping

public class ControllerClassNameHandlerMapping extends AbstractControllerUrlHandlerMapping

org.springframework.context.support.ApplicationObjectSupport
 
 
org.springframework.web.context.support.WebApplicationObjectSupport
  
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
   
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
    
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
     
 
org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping
      
 
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping

Implementation of HandlerMapping that follows a simple convention for generating URL path mappings from the class names of registered Controller beans as well as @Controller annotated beans.

HandlerMapping实例,它根据一个简单的规则来生成一个url路径映射,这个映射来自一个注册的控制器的class名称,以及它的@Controller注释。

For simple Controller implementations (those that handle a single request type), the convention is to take the short name of the Class, remove the 'Controller' suffix if it exists and return the remaining text, lower-cased, as the mapping, with a leading /. For example:

为简单的控制器实现(处理一个单个的请求),这个规则是取得类的短名称(就是不包括包名),去掉'Controller'后缀的剩余部分,转换为小写,再在前面加/。例如:

WelcomeController -> /welcome*

HomeController -> /home*

For MultiActionControllers and @Controller beans, a similar mapping is registered, except that all sub-paths are registered using the trailing wildcard pattern /*. For example:

         对于MultiActionControllers和@Controller bean,类似的映射被注册,除了是用/*注册所有子路径。例如:

WelcomeController -> /welcome, /welcome/*

CatalogController -> /catalog, /catalog/*

For MultiActionController it is often useful to use this mapping strategy in conjunction with the InternalPathMethodNameResolver.

         对于MultiActionController来说,使用这个映射策略往往是有益的,并结合使用InternalPathMethodNameResolver。

Set方法

void  setBasePackage(String basePackage)

Set the base package to be used for generating path mappings, including all subpackages underneath this packages as path elements.

void  setCaseSensitive(boolean caseSensitive)

Set whether to apply case sensitivity to the generated paths, e.g. turning the class name "BuyForm" into "buyForm".

void  setPathPrefix(String prefixPath)

Specify a prefix to prepend to the path generated from the controller name.

 

A.1.1.2.3、DefaultAnnotationHandlerMapping

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandlerMapping

org.springframework.context.support.ApplicationObjectSupport
 
 
org.springframework.web.context.support.WebApplicationObjectSupport
  
 
org.springframework.web.servlet.handler.AbstractHandlerMapping
   
 
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
    
 
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
     
 
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

Implementation of the HandlerMapping interface that maps handlers based on HTTP paths expressed through the RequestMapping annotation at the type or method level.

HandlerMapping接口的一个实现,它是基于HTTP路径映射处理,通过RequestMapping在类型和方法级别的注释。

Registered by default in DispatcherServlet on Java 5+. NOTE: If you define custom HandlerMapping beans in your DispatcherServlet context, you need to add a DefaultAnnotationHandlerMapping bean explicitly, since custom HandlerMapping beans replace the default mapping strategies. Defining a DefaultAnnotationHandlerMapping also allows for registering custom interceptors:

在java 5以上版本中,它是DispatcherServlet中的默认注册。注意,若你在你的DispatcherServlet上下文中定义了自定义的HandlerMapping,你需要添加一个明确的DefaultAnnotationHandlerMapping bean,因为自定义的HandlerMapping bean替换了默认的映射策略。定义一个DefaultAnnotationHandlerMapping还允许注册自定义拦截器。

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<property name="interceptors">

...

</property>

</bean>

Annotated controllers are usually marked with the Controller stereotype at the type level. This is not strictly necessary when RequestMapping is applied at the type level (since such a handler usually implements the Controller interface). However, Controller is required for detecting RequestMapping annotations at the method level if RequestMapping is not present at the type level.

注释控制器通常在类型级别通过Controller stereotype来标记。当RequestMapping应用在类型级别时,这不是绝对必须的(因为这样的处理程序通常实现Controller接口)。然而,若RequestMapping在类型级别中不存在,控制器需要在方法级别检查RequestMapping的注释。

NOTE: Method-level mappings are only allowed to narrow the mapping expressed at the class level (if any). HTTP paths need to uniquely map onto specific handler beans, with any given HTTP path only allowed to be mapped onto one specific handler bean (not spread across multiple handler beans). It is strongly recommended to co-locate related handler methods into the same bean.

注意,方法级别映射仅仅允许缩小在类级别所表示的映射。HTTP路径需要一个唯一的映射到特定的处理bean(一个HTTP不能映射到多个处理bean)。

The AnnotationMethodHandlerAdapter is responsible for processing annotated handler methods, as mapped by this HandlerMapping. For RequestMapping at the type level, specific HandlerAdapters such as SimpleControllerHandlerAdapter apply.

AnnotationMethodHandlerAdapter是负责处理注释处理方法。

Set方法

void  setUseDefaultSuffixPattern(boolean useDefaultSuffixPattern)

Set whether to register paths using the default suffix pattern as well: i.e. whether "/users" should be registered as "/users.*" too.

 

B、HandlerInterceptor(拦截器)

org.springframework.web.servlet.HandlerInterceptor

public interface HandlerInterceptor

Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.

工作流接口,它允许自定义处理程序的执行链。应用程序可以为处理器注册一组现存的或自定义的拦截器,可以添加常用的预处理行为而不用修改任何处理器的实现。

A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, e.g. for authorization checks, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.

在相应的HandlerAdapter触发执行处理器本身之前,调用一个HandlerInterceptor。这种机制可以用于预处理方面,例如,授权检查,公共处理行为如locale或theme改变。其主要目的是分解出重复处理的代码。

Typically an interceptor chain is defined per HandlerMapping bean, sharing its granularity. To be able to apply a certain interceptor chain to a group of handlers, one needs to map the desired handlers via one HandlerMapping bean. The interceptors themselves are defined as beans in the application context, referenced by the mapping bean definition via its "interceptors" property (in XML: a <list> of <ref>).

拦截器通常是由HandlerMapping定义的。为了把一组拦截器应用到一组处理器上,需要通过一个HandlerMapping bean来映射到一个处理器上。这个拦截器本身被定义为一个在应用程序上下文中的bean,在HandlerMapping中通过定义interceptors属性来引用拦截器。

HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.

HandlerInterceptor和2.3 Filter类似,但是和后者相比,它只允许定义通过禁止处理器本身执行的选项的预处和自定义后处理。Filter功能更强大,例如,它允许交换请求和响应对象。注意,过滤器(Filter)被配置在web.xml中,HandlerInterceptor在应用程序上下文中定义。

As a basic guideline, fine-grained handler-related preprocessing tasks are candidates for HandlerInterceptor implementations, especially factored-out common handler code and authorization checks. On the other hand, a Filter is well-suited for request content and view content handling, like multipart forms and GZIP compression. This typically shows when one needs to map the filter to certain content types (e.g. images), or to all requests.

 

处理拦截器定义了三个方法,一个是在controller执行之前被调用,一个是在controller执行之后被调用,一个是在整个请求处理完成之后被调用。

HandlerInterceptor配置范例

<bean id="SimpleUrlHM2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

       <list>

           <ref bean="" />

           <!-- 此处定义处理拦截器的bean -->

       </list>

    </property>

    <property name="mappings">

       <props>

           <prop key="/user/login.do">loginController</prop>

       </props>

    </property>

</bean>

 

B.1、HandlerInterceptorAdapter

org.springframework.web.servlet.handler.HandlerInterceptorAdapter

public abstract class HandlerInterceptorAdapter implements HandlerInterceptor

java.lang.Object
  
 
org.springframework.web.servlet.handler.HandlerInterceptorAdapter

Abstract adapter class for the HandlerInterceptor interface, for simplified implementation of pre-only/post-only interceptors.

HandlerInterceptor接口的抽象类,是pre-only/post-only拦截器的简单实现。

我们可以自定义一个继承于抽象类HandlerInterceptorAdapter的类来实现。

 

B.1.1、LocaleChangeInterceptor

org.springframework.web.servlet.i18n.LocaleChangeInterceptor

public class LocaleChangeInterceptor extends HandlerInterceptorAdapter

java.lang.Object
  
 
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
      
 
org.springframework.web.servlet.i18n.LocaleChangeInterceptor

Interceptor that allows for changing the current locale on every request, via a configurable request parameter.

它允许在每个请求中改变当前locale,由一个可定义的请求参数。

 

B.1.2、ThemeChangeInterceptor

org.springframework.web.servlet.theme.ThemeChangeInterceptor

public class ThemeChangeInterceptor extends HandlerInterceptorAdapter

java.lang.Object
  
 
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
      
 
org.springframework.web.servlet.theme.ThemeChangeInterceptor

Interceptor that allows for changing the current theme on every request, via a configurable request parameter.

它允许在每个请求中改变当前theme,由一个可定义的请求参数。

 

B.1.3、UserRoleAuthorizationInterceptor

org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor

public class UserRoleAuthorizationInterceptor extends HandlerInterceptorAdapter

Interceptor that checks the authorization of the current user via the user's roles, as evaluated by HttpServletRequest's isUserInRole method.

这个拦截器它通过用户权限来检查当前用户的授权,它通过HttpServletRequest的isUserInRole方法来评估。

 

B.1.4、ConversionServiceExposingInterceptor

org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor

public class ConversionServiceExposingInterceptor extends HandlerInterceptorAdapter

Interceptor that places the configured ConversionService in request scope so it's available during request processing. The request attribute name is "org.springframework.core.convert.ConversionService", the value of ConversionService.class.getName().

它放置在请求范围内配置的ConversionService。

 

B.2、WebContentInterceptor

org.springframework.web.servlet.mvc.WebContentInterceptor

public class WebContentInterceptor extends WebContentGenerator implements HandlerInterceptor

Interceptor that checks and prepares request and response. Checks for supported methods and a required session, and applies the specified number of cache seconds. See superclass bean properties for configuration options.

这个拦截器检查和准备请求和响应。检查支持的方法,所需的session和缓存指定描述。

All the settings supported by this interceptor can also be set on AbstractController. This interceptor is mainly intended for applying checks and preparations to a set of controllers mapped by a HandlerMapping.

         这个拦截器所支持的属性也可以用于设定AbstractController。这个拦截器主要用于检查和准备HandlerMapping映射的一组控制器。

 

B.2、WebRequestHandlerInterceptorAdapter

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有