SpringBoot整合web应用
整合Servlet
整合Filter
整合Listener
访问静态资源
文件上传
一、整合Servlet
spring 3.0版本以下 整合 servlet
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.alan.servlet.FristServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
1、通过注解扫描的方式完成Servlet 组件注册
1.1编写Servlet
@WebServlet(name="FirstServlet",urlPatterns="/first") public class FirstServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("编写一个Servlet。。。。。。。。。。。。。。。。。"); // TODO Auto-generated method stub super.doGet(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); }
}
1.2编写springboot启动器
@WebServlet(name="FirstServlet",urlPatterns="/first") public class FirstServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("编写一个Servlet。。。。。。。。。。。。。。。。。"); // TODO Auto-generated method stub super.doGet(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); }
}
2.通过方法完成servlet 组件的注册
2.1编写servlet
/** * 通过方法完成servlet 组件的注册 * @author Administrator * */ public class SecondServlet extends HttpServlet{
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("通过方法完成servlet 组件的注册。。。。。。。。。。。。。。。。。"); super.doPost(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); }
}
2.2、编写启动类
/** * spring boot 启动器 通过方法完成servlet 组件的注册 * @author Administrator * */ @SpringBootApplication public class App2 { public static void main(String args[]){ SpringApplication.run(App2.class, args); } @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean<SecondServlet> servletRegistrationBean = new ServletRegistrationBean(new SecondServlet()); servletRegistrationBean.addUrlMappings("/second"); return servletRegistrationBean; }
}
二、整合Filter
1、通过注解扫描的方式完成Filter 组件的注册
1.1编写Filter
/** * * @author Administrator *SpringBoot 整合Filter 方式一 通过注解扫描的方式完成Filter组件的注册 *<filter> *<filter-name>FirstFilter</filter-name> *<filter-class>com.alan.filter.FirstFilter</filter-class> *</filter> *<filter-mapping> *<filter-name>FirstFilter</filter-name> *<url-pattern>/firstFilter</url-pattern> *</filter-mapping> * * * */ //@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"}) 配置多个请求后缀方式 @WebFilter(filterName="FirstFilter",urlPatterns="/firstFilter") public class FirstFilter implements Filter {
@Override public void destroy() { // TODO Auto-generated method stub Filter.super.destroy(); }
@Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("进入Filter。。。。。。。。。。。。。"); arg2.doFilter(arg0, arg1); System.out.println("离开Filter。。。。。。。。。。。。。"); // TODO Auto-generated method stub }
@Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub Filter.super.init(filterConfig); }
}
1.2编写启动类
/** * SpringBoot 启动类 * @author Administrator * */
@SpringBootApplication @ServletComponentScan public class AppFilter { public static void main(String args[]){ SpringApplication.run(AppFilter.class, args); } }
2、通过方法完成Filter组件的注册
2.1 编写Filter
/** * 方式二通过方法完成Filter 组件的注册 * @author Administrator * */ public class SecondFilter implements Filter {
@Override public void destroy() { // TODO Auto-generated method stub Filter.super.destroy(); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("进入SecondFilter。。。。。。。。。。。"); chain.doFilter(request, response); System.out.println("离开SecondFilter。。。。。。。。。。。。。"); }
@Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub Filter.super.init(filterConfig); }
}
2.2 编写启动类
/** * springboot 启动器 * 方式二:通过方法完成Filter组件的注册 * @author Administrator * */ @SpringBootApplication public class AppSecondFilter {
public static void main(String args[]){ SpringApplication.run(AppSecondFilter.class, args); } @Bean public FilterRegistrationBean<SecondFilter> getRegistrationFilterBean(){ FilterRegistrationBean<SecondFilter> registrationBean = new FilterRegistrationBean<SecondFilter>(new SecondFilter()); registrationBean.addUrlPatterns("/secondFilter","/first");//拦截多个路径 return registrationBean; } }
三、SpringBoot整合Listener
1.通过注解扫描的方式完成Listener组件的注册
1.1编写Listener
/** * spingboot 整合Listener * * <listener> * <listener-class>com.alan.listener.FirstListener</listener-class> * </listener> * @author Administrator * */ @WebListener public class FirstListener implements ServletContextListener{
@Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub ServletContextListener.super.contextDestroyed(sce); }
@Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("通过扫描注解监听进入。。。。。。。。。。。。。。。"); ServletContextListener.super.contextInitialized(sce); } }
1.2编写启动类
/** * springboot 启动器 * @author Administrator * */ @SpringBootApplication public class AppListenerApplicationOne { public static void main(String args[]){ SpringApplication.run(AppListenerApplicationOne.class, args); } }
2.通过方法完成Listener组件的注册
2.1编写Listener
/** * 编写监听器 * @author Administrator * */ public class SecondListener implements ServletContextListener {
@Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub ServletContextListener.super.contextDestroyed(sce); }
@Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("通过方法扫描的方式进入监听。。。。。。。。。。。。"); ServletContextListener.super.contextInitialized(sce); }
}
2.2编写启动类
/** * springBoot 启动器 * springboot 整合listener 方式二 * @author Administrator * */ @SpringBootApplication public class AppListenerApplicationSecond {
public static void main(String args[]){ SpringApplication.run(AppListenerApplicationSecond.class, args); } @Bean public ServletListenerRegistrationBean<SecondListener> getListenerRegintrationBean(){ ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener()); return bean; } }
四 SpringBoot 访问静态资源
1.SpringBoot 从 classpath/static 的目录访问静态资源
注意:静态资源存放的目录名称必须为static
2.ServletContext 根目录下
在src/main/webapp 目录名称必须为webapp
五 springboot 上传文件
1、编写文件上传controller类
/** * 定义文件上传的 controller * @author Administrator * */ @RestController//表示该类下的方法返回值默认为json格式 public class UploadFileController { @PostMapping("/fileUpload") public Map<String,Object> uploadFile(MultipartFile filename) throws Exception{ Map<String,Object> map = new HashMap<String,Object>(); System.out.println(filename.getOriginalFilename());//输出文件的源文件名称 filename.transferTo(new File("e:/"+filename.getOriginalFilename())); map.put("msg", "ok"); return map; } }
2、编写页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>上传文件</title> </head> <body> <h1>文件上传</h1>
<form action="fileUpload" enctype="multipart/form-data" method="post"> 选择文件:<input name="filename" type="file" /> <input type="submit" value="提交" /> </form> </body> </html>
3、配置application.properties 文件,声明文件上传大小和编码格式
#设置单个文件上传大小限制 spring.http.multipart.maxFileSize=200M
#设置一次请求传输文件的最大限制 spring.http.multipart.maxRequestSize=200M
#解决静态资源中文乱码问题 spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
4、编写程序启动类
/** * 定义程序 启动类 * @author Administrator * */ @SpringBootApplication public class uploadApp { public static void main(String args[]){ SpringApplication.run(uploadApp.class, args); } }