自定义拦截器概述
pringMVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器可以实现HandlerInterceptor接口,或者可以继承
HandlerInterceptorAdapter 适配器类
- preHandler():这个方法再业务处理器处理请求之前被调用,再该方法中对用户请求request进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
- postHandler():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用。在该方法中对用户请求request进行处理。
- *afterCompletion():在 DispatcherServlet 完全处理完请求后被调用。可以在该方法中进行一些资源清理的操作。
单个拦截器
public class FirstHandlerInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-postHandle");
}
@Override
/*
* 返回true:
* com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-preHandle
* com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-postHandle
* com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-afterCompletion
*返回false:
* com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-preHandle
* */
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println(this.getClass().getName() + "-preHandle");
return false;
}
}
- 配置文件
<mvc:interceptors>
<bean id="FirstHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor"></bean>
</mvc:interceptors>
多个拦截器
public class FirstHandlerInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-postHandle");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println(this.getClass().getName() + "-preHandle");
return false;
}
}
public class FirstHandlerInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println(this.getClass().getName() + "-postHandle");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println(this.getClass().getName() + "-preHandle");
return true;
}
}
- 配置文件
<mvc:interceptors>
<bean id="FirstHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor"></bean>
<bean id="SecondHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.SecondHandlerInterceptor"></bean>
</mvc:interceptors>