一:配置过滤器
1.创建TestFilter类并实现Filter接口
@WebFilter(filterName="testFilter",urlPatterns="/*") //@WebFilter是定义过滤器的注解 ,urlPatterns="/*" 定义过滤器过滤的路径
public class TestFilter implements Filter{
@Override
public void destroy() {
System.out.println("过滤器被销毁");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("过滤器被执行");
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("过滤器被初始化");
}
}
2.需要在启动类中添加@ServletComponentScan注解@WebFilter注解方可生效
@ServletComponentScan(basePackages = {"www.zxf.com.filter","www.zxf.com.listener"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
二:配置监听器
1.创建TestListener类并实现ServletContextListener接口
@WebListener //将该类定义为一个监听器
public class TestListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("监听器被销毁");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("监听器被创建");
}
}
2.需要在启动类中添加@ServletComponentScan注解@WebListener注解方可生效
@ServletComponentScan(basePackages = {"www.zxf.com.filter","www.zxf.com.listener"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
三:配置拦截器
1.创建拦截器一
package www.zxf.com.intercepter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class IntercepterTest1 implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("请求执行后1");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("请求执行后视图渲染前1");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("请求执行之前1");
return true;
}
}
2.创建拦截器二
package www.zxf.com.intercepter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class IntercepterTest2 implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("请求执行后2");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("请求执行后视图渲染前2");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("请求执行之前2");
return true;
}
}
3.配置拦截器并将拦截器一和拦截器二加入该配置中
package www.zxf.com.intercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebAdepter extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new IntercepterTest1()).addPathPatterns("/**");//配置拦截路径
registry.addInterceptor(new IntercepterTest2()).addPathPatterns("/**");//配置拦截路径
super.addInterceptors(registry);
}
}