译文: <<Spring MVC HandlerInterceptor Example>>

Spring MVC Handler拦截器用例

Spring MVC HandlerInterceptor Example


在这里我将使用 WebMvcConfigurerAdapter 编写一个 HandlerInterceptor 的用例.
Spring HandlerInterceptor 与 Servlet 过滤器 类似, Servlet 过滤器更强大些, 它只能在 web.xml 中配置( 正如对 DispatcherServlet 的配置一样, 译者注 ), 而 HandlerInterceptor 可通过 Spring 应用上下文 xml 文件和 Java 代码配置,

On this page we will provide Spring MVC HandlerInterceptor example with WebMvcConfigurerAdapter. Spring HandlerInterceptor works similar to the Servlet Filter. Servlet filters are more powerful than Spring HandlerInterceptor. Servlet Filters are configured in web.xml and Spring HandlerInterceptor is configured in Spring application context xml file or Java configuration. Spring HandlerInterceptor can be used to avoid repetitive handler code. We can use HandlerInterceptor for different purposes like authorization checks, locale checks etc. In java configuration, WebMvcConfigurerAdapter has a method as addInterceptors in which InterceptorRegistry is passed as an argument. Using InterceptorRegistry.addInterceptor(), we add our interceptors and using InterceptorRegistry.addInterceptor().addPathPatterns(), we provide path pattern specific to given interceptor. If an interceptor has not been provided with any path pattern, it is called for each request. Spring provides HandlerInterceptorAdapter adapter class for implementing only required handler methods of HandlerInterceptor. HandlerInterceptor has three methods.

猜你喜欢

转载自www.cnblogs.com/develon/p/11503814.html