详解IDEA中SpringBoot整合Servlet三大组件的过程
SpringMVC整合
SpringBoot提供为整合MVC框架提供的功能特性
- 内置两个视图解析器:ContentNegotiatingViewResolver和BeanNameViewResolver
- 支持静态资源以及WebJars
- 自动注册了转换器和格式化器
- 支持Http消息转换器
- 自动注册了消息代码解析器
- 支持静态项目首页index.html
- 支持定制应用图标favicon.ico
- 自动初始化Web数据绑定器:ConfigurableWebBindingInitializer
SpringMVC功能扩展实现
- 项目环境搭建(结构如这篇博客)
- 功能扩展实现
- 注册视图管理器
/* 在config文件夹下编写配置类 实现WebMvcConfigurer接口,扩展MVC功能 测试前将LoginController控制类注释,更好的观察效果 */ @Configuration publicclassMyMVCConfigimplementsWebMvcConfigurer{ //添加视图管理 @Override publicvoidaddViewControllers(ViewControllerRegistryregistry){ //请求toLoginPage映射路径或者login.html页面都会自动映射到login.html页面 registry.addViewController("/toLoginPage").setViewName("login"); registry.addViewController("/login.html").setViewName("login"); } }
-测试后发现,使用这种方式无法获取后台处理的数据,比如登录页面中的年份。
-使用WebMvcConfigurer接口中的addViewControllers(ViewControllerRegistryregistry)方法定制
视图控制,只适合较为简单的无参数视图Get方式的请求跳转,对于有参数或需要业务处理的跳转请求,最好还是采用传统方式处理请求。
注册自定义拦截器
/* 自定义一个拦截器类,实现简单的拦截业务 */ @Configuration publicclassMyInterceptorimplementsHandlerInterceptor{ @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ //用户请求/admin开头路径时,判断用户是否登录 Stringuri=request.getRequestURI(); ObjectloginUser=request.getSession().getAttribute("loginUser"); if(uri.startsWith("/admin")&&null==loginUser){ response.sendRedirect("/toLoginPage"); returnfalse; } returntrue; } @Override publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,@NullableModelAndViewmodelAndView)throwsException{ //向request域中存放当前年份用于页面动态展示 request.setAttribute("currentYear",Calendar.getInstance().get(Calendar.YEAR)); } @Override publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{ } }
-自定义拦截器类MyInterceptor实现了HandlerInterceptor接口。在preHandle()方法中,如果用户请求以“/admin”开头,
则判断用户是否登录,如果没有登录,则重定向到“/toLoginPage”请求对应的登录页面。
-在postHandle()方法中,使用request对象向前端页面传递表示年份的currentYear数据。
-在自定义配置类MyMVCConfig中,重写addInterceptors()方法注册自定义的拦截器,如下
@Autowired privateMyInterceptormyInterceptor; //添加拦截器管理 @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ registry.addInterceptor(myInterceptor) .addPathPatterns("/**") .excludePathPatterns("/login.html"); }
-使用@Autowired注解引入自定义的MyInterceptor拦截器组件,重写其中addInterceptors()方法注册自定义的拦截器
-使用addPathPatterns("/**")方法拦截所有路径请求,excludePathPatterns("/login.html")方法对“login.html”路径请求放行处理。
-项目重启后,访问localhost:8080/admin,跳转到登录界面,自定义拦截器生效。
SpringBoot整合Servlet三大组件
组件注册方式整合Servlet三大组件
在SpringBoot中,使用组件注册方式整合内嵌Servlet容器的Servlet、Filter、Listener三大组件时,
只需要将这些自定义组件通过ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean类注册到容器中即可
组件注册方式整合Servlet
/* 自定义Servlet类 使用@Component注解将MyServlet类作为组件注入Spring容器。该类继承自HTTPServlet, 通过HttpServletResponse对象向页面输出"helloMyServlet" */ @Component publicclassMyServletextendsHttpServlet{ @Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ this.doPost(req,resp); } @Override protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ resp.getWriter().write("helloMyServlet"); } }
/* 嵌入式Servlet容器三大组件配置 @Configuration注解将该类标注为配置类,getServlet()方法用于注册自定义MyServlet, 返回ServletRegistrationBean类型的Bean对象 */ @Configuration publicclassServletConfig{ //注册Servlet组件 @Bean publicServletRegistrationBeangetServlet(MyServletmyServlet){ returnnewServletRegistrationBean (myServlet,"/myServlet"); } }
启动测试,访问myServlet,显示数据说明成功整合Servlet组件
组件注册方式整合Filter
/* 自定义Filter类 使用@Component注解将当前MyFilter类作为组件注入到Spring容器中 MyFilter类实现Filter接口,重写如下三个方法,在doFilter()方法中想控制台打印"helloMyFilter" */ @Component publicclassMyFilterimplementsFilter{ @Override publicvoidinit(FilterConfigfilterConfig)throwsServletException{ } @Override publicvoiddestroy(){ } @Override publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{ System.out.println("helloMyFilter"); filterChain.doFilter(servletRequest,servletResponse); } }
//注册Filter组件 @Bean publicFilterRegistrationBeangetFilter(MyFiltermyFilter){ FilterRegistrationBean registrationBean=newFilterRegistrationBean<>(myFilter); registrationBean.setUrlPatterns(Arrays.asList("/toLoginPage","/myFilter")); returnregistrationBean; }
启动测试,访问/myFilter,控制台看到helloMyFilter
组件注册方式整合Listener
/* 自定义Listener类 */ @Component publicclassMyListenerimplementsServletContextListener{ @Override publicvoidcontextInitialized(ServletContextEventsce){ System.out.println("contextInitialized..."); } @Override publicvoidcontextDestroyed(ServletContextEventsce){ System.out.println("contextDestroyed..."); } }
//注册Listener组件 @Bean publicServletListenerRegistrationBeangetServletListener(MyListenermyListener){ returnnewServletListenerRegistrationBean<>(myListener); }
程序启动成功后,会自动打印输出"contextInitialized...",单击坐下的Exit关闭会输出销毁的监听信息,如果直接强制关闭程序,无法打印监听信息。
- 注意:当自定义的Servlet组件配置类ServletConfig全部注释并重启项目后,自定义的Servlet、Filter、Listener组件仍然生效。
- 原因:嵌入式Servlet容器对Servlet、Filter、Listener组件进行了自动化识别和配置,而自定义的Servlet、Filter、Listener都继承/实现了对应的类/接口,同时自定义的这三个组件都使用了@Component注解,会自动被扫描为Spring组件。
路径扫描整合Servlet三大组件
- 使用路径扫描的方式整合三大组件,需要再自定义组件上分别添加@WebServlet、@WebFilter、@WebListener注解进行声明,并配置相关注解属性,在主程序启动类上使用@ServletComponentScan注解开启组件扫描。
- 分别用以下三个注解代替@Component注解进行配置三个组件@WebFilter(value={"/antionLogin","/antionMyFilter"})
- @WebListener
- @WebServlet("/annotationServlet")
- 启动类上加入@ServletComponentScan注解,开启基于注解的组件扫描支持
- 对于Filter测试访问"/antionLogin","/antionMyFilter",对于Servlet测试访问"/annotationServlet",测试结果如上。
到此这篇关于详解IDEA中SpringBoot整合Servlet三大组件的过程的文章就介绍到这了,更多相关SpringBoot整合Servlet三大组件内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!