标签:add inter except pat 执行 自定义拦截器 object servlet obj
自定义拦截器实现HandlerInterceptor接口的三个方法。
public class MyInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//拦截内容
//放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//请求来时执行
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//响应来时执行
}
}
//将拦截器放入mvc容器中
@Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
//添加拦截器,并配置拦截路径
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置拦截器
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
}
标签:add inter except pat 执行 自定义拦截器 object servlet obj
原文地址:https://www.cnblogs.com/ygfcoder/p/13411963.html