Spring-MVC如何使用拦截器,今天看了一下官方文档,没有找到注解的例子,只给出了非注解风格的例子。基于注解风格如何使用拦截器呢?
基于注解有两个可以使用的定义类,分别是DefaultAnnotationHandlerMapping
和
AnnotationMethodHandlerAdapter
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
DefaultAnnotationHandlerMapping本身支持自定义拦截器,配置如下
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.chenzhou.blog.comment.interceptor.ExceptionInterceptor"/> </list> </property> </bean>
Interceptor的定义如下:
public class ExceptionInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub return super.preHandle(request, response, handler); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub super.postHandle(request, response, handler, modelAndView); } }
在AnnotationMethodHandlerAdapter中还没有找到如何配置自定义Interceptor的方法!