SpringMvc自定义拦截器(注解)代码实例
拦截器
自定义拦截器实现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 {
//响应来时执行
}
}
springmvc配置文件
//将拦截器放入mvc容器中
@Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
//添加拦截器,并配置拦截路径
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置拦截器
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
}
以上是 SpringMvc自定义拦截器(注解)代码实例 的全部内容, 来源链接: utcz.com/z/327911.html