博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc 的配置 及interceptor filter listener servlet 配置
阅读量:4973 次
发布时间:2019-06-12

本文共 3638 字,大约阅读时间需要 12 分钟。

创建 三个类 

分别实现 Filter  ServletContextListener  HttpServlet

在springboot 启动类中@bean加入

2 ,实现 ServletContextIntializer接口 

@SpringBootApplicationpublic class DemoApplication implements ServletContextInitializer{    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }        @Override    public void onStartup(ServletContext servletContext) throws ServletException {        servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/root");        servletContext.addFilter("customfilter", new CustomFilter())                        .addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet");        servletContext.addListener(new CustomListener());    }}

3. 注解方式  主类加上@ServletComponentScan     给三个分别加上注解   

@WebServlet(urlPatterns="/index", name="customServlet")public class CustomServlet extends HttpServlet{   ....}@WebListenerpublic class CustomListener implements ServletContextListener{   ....  }@WebFilter(urlPatterns="/*")public class CustomFilter implements Filter{  ...  }

 

4.   自定义注解 实现  主类上加上此注解

@Target(value = {ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Import(value = {FirstFilterConfiguration.class})public @interface EnableFirstFilter {}

注册配置的bean(

FirstFilterConfiguration.class

public class FirstFilterConfiguration {    public static final Integer ORDER = Integer.MIN_VALUE;            @Bean    public FilterRegistrationBean firstFilter(){
  // 默认不需要的但是为了拦截过滤固定的页面 FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new FirstFilter()); registration.addUrlPatterns("/*"); registration.setName("firstFilter"); registration.setOrder(ORDER); return registration; }}
public class FirstFilter implements Filter {    @Override    public void destroy() {            }    @Override    public void doFilter(ServletRequest srst, ServletResponse srsp, FilterChain chain)            throws IOException, ServletException {        chain.doFilter(srst, srsp);    }    @Override    public void init(FilterConfig arg0) throws ServletException {            }}
@Configuration@EnableWebMvcpublic class WebMvcConfig extends WebMvcConfigurerAdapter{    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        //addResourceHandler  对外暴露的路径       addResourceLocations 对内暴露路径        registry.addResourceHandler("/aaserts/**").addResourceLocations("classpath:/assets/");    }    @Override    public void addViewControllers(ViewControllerRegistry registry) {        registry.addViewController("/ws").setViewName("/ws");         registry.addViewController("/login").setViewName("/login");         registry.addViewController("/chat").setViewName("/chat");    }        @Override    public void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(demoInterceptor());    }        @Bean    public DemoInterceptor demoInterceptor() {        return new DemoInterceptor();    }        //自定义拦截器    public class DemoInterceptor extends HandlerInterceptorAdapter{                @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 {            super.postHandle(request, response, handler, modelAndView);        }    }}

 

 拦截器

 

转载于:https://www.cnblogs.com/mxz1994/p/8215393.html

你可能感兴趣的文章
Vue2.0组织浏览器返回事件
查看>>
DBeauty 2.0.11 发布,数据库关系浏览器
查看>>
Linux Deepin 新增北理工等3处新镜像服务
查看>>
Attic 0.8.1 发布,备份程序
查看>>
Hanoi
查看>>
XSS的知识普及和预防办法
查看>>
Java疯狂讲义
查看>>
更改大商创立即购买【仿淘宝】
查看>>
CentOS 7 nfs客户端挂载问题
查看>>
OkHttp2.0有Bug,暂时不推荐在产品中使用
查看>>
【HANA系列】SAP HANA SQL查找字符串位置
查看>>
Leetcode:Best Time to Buy and Sell Stock
查看>>
HYSBZ 1901 Dynamic Rankings
查看>>
IE8下jQuery改变png图片透明度时出现的黑边问题
查看>>
FTP两种工作模式:主动模式(Active FTP)和被动模式介绍
查看>>
Promise
查看>>
iphone开发视频教程下载
查看>>
javascript - 一种对象赋值方式
查看>>
闰年测试
查看>>
jQuery的一些笔记
查看>>