SpringMvc学习笔记六

拦截器

1. 拦截器类

@Component
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle ...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle ...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion ...");
    }
}


@Component
public class ProjectInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle222 ...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle222 ...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion222 ...");
    }
}

  • 加上Component 注解 ,注入到Spring中

2. 使用方式

2.1  方式一

2.1.1  SpringMvcSupport配置类

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {

    @Autowired
    public ProjectInterceptor projectInterceptor;

    @Autowired
    private ProjectInterceptor2 projectInterceptor2;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
    }


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
    }
}
  • 加上@Configruation 注解
  • 继承WebMvcConfigurationSupport 
  • 重写 addInterceptors 方法 注册需要拦截的路径

2.1.2  配置SpringMvcConfig 

@Configuration
@ComponentScan({"com.itheima.controller", "com.itheima.config"})
@EnableWebMvc     //使用json需要加上这个注解开启json序列化
public class SpringMvcConfig implements{
}
  • 需要在@ComponentScan 扫描路径中添加上com.itheima.conf

2.2 方式二

@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc     //使用json需要加上这个注解开启json序列化
public class SpringMvcConfig implements WebMvcConfigurer {

    //方式二实现拦截器 实现WebMvcConfigurer接口, 实现addInterceptors方法
    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
    }
}
  • SpringMvcConfig 类 实现WebMvcConfigurer接口
  • 重写addInterceptor方法

你可能感兴趣的:(java,学习,笔记,java)