个人是这么理解的,类似laravel
中间件的作用,可以前置可以后置。
@Configuration
public class MyAdapter extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry){
//添加拦截器
registry.addInterceptor(new MyInterceptor())
.addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
super.addInterceptors(registry);
}
}
粗体斜体较为重要,super表示重写父类的方法。 具体的方法在new MyInterceptor()这个类实现。
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//方法体
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//方法体
}
}