SpringBoot整合Web的简单使用总结,包括整合Servlet,FilterS,Listener以及访问静态资源和文件上传。
SpringBoot整合Servlet有两种方式:
方式一:通过注解扫描完成Servlet组件的注册
servlet
package com.example.servlet;
/**
* springBoot整合servlet方式一
*/
@WebServlet(name="FirstServlet",urlPatterns="/first") //相当于web.xml中的配置
public class FirstServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("第一个servlet");
}
}
启动器
package com.example;
/**
* springBoot整合servlet方式一
*
*/
@SpringBootApplication //表示该类为启动类
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并且实例化
public class AppServlet01 {
public static void main(String[] args) {
SpringApplication.run(AppServlet01.class, args);
}
}
方式二:通过方法完成Servlet组件的注册
servlet
package com.example.servlet;
/**
* springBoot整合servlet方式二
*/
@WebServlet(name="SecondServlet",urlPatterns="/second")
public class SecondServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("第二个servlet");
}
}
启动器
package com.example;
/**
* springBoot整合servlet方式二
*/
@SpringBootApplication
public class AppServlet02 {
public static void main(String[] args) {
SpringApplication.run(AppServlet02.class, args);
}
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
SpringBoot整合Filter有两种方式:
方式一:通过注解扫描完成Filter组件注册
Filter
package com.example.filter;
/**
* springBoot整合Filter方式一
* <filter>
* <filter-name>FirstFilter</filter-name>
* <filter-class>com.zll.filter.FirstFilter</filter-class>
* </filter>
* <filter-mapping>
* <filter-name>FirstFilter</filter-name>
* <url-pattern>/first</url-pattern>
* </filter-mapping>
*/
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})urlPatterns配置的是拦截内容
@WebFilter(filterName="FirstFilter",urlPatterns={"/first"}) //拦截FirstServlet
public class FirstFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@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");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
启动器
package com.example;
/**
* SpringBoot整合Filter方式一
* @author zll
*
*/
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并且实例化
public class AppFilter01 {
public static void main(String[] args) {
SpringApplication.run(AppFilter01.class, args);
}
}
方式一:通过方法完成Filter组件注册
Filter
package com.example.filter;
/**
* springBoot整合filter的第二种方式
*
*/
@WebFilter()
public class SecondFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@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");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
启动器
package com.example;
/**
* springBoot 整合filter方式二
*
*/
@SpringBootApplication
@ServletComponentScan
public class AppFilter02 {
public static void main(String[] args) {
SpringApplication.run(AppFilter02.class, args);
}
/**
* 注册servlet
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean =new FilterRegistrationBean(new SecondFilter());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}
SpringBoot整合Listener有两种方式:
方式一:通过注解扫描完成Listener组件注册
Listener
package com.example.listener;
/**
* springBoot整合Listener
*/
@WebListener
public class FirstListerner implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("第一个监听器初始化");
}
}
启动器
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* springBoot整合Listener方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class AppListener01 {
public static void main(String[] args) {
SpringApplication.run(AppListener01.class, args);
}
}
方式二:通过方法完成Listener组件注册
Listener
package com.example.listener;
/**
* springBoot整合Listener方式二
*/
public class SecondListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("第二个监听器开始启动");
}
}
启动器
package com.example;
/**
* SpringBoot整合Listener方式二
*/
@SpringBootApplication
public class AppListener02 {
public static void main(String[] args) {
SpringApplication.run(AppListener02.class, args);
}
/**
* 注册监听器
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean=new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
SpringBoot访问静态资源的两种方式:
方式一:SpringBoot从classpath/static目录下访问(注意:目录名称必须是static)
![静态资源访问方式一](https://i-blog.csdnimg.cn/blog_migrate/8311a85d24429494b92360d86cdc411e.png)
方式二:SpringBoot在src/main/webapp目录下访问静态资源(注意:目录名称必须要是webapp)
![](https://i-blog.csdnimg.cn/blog_migrate/45d4d3472c0e4dbb6e48f2c146ecb7f3.png)
SpringBoot实现文件上传:
Controller
package com.example.controller;
/**
* SpringBoot文件上传
*/
@RestController //表示该类下的方法的返回值会自动做json格式的转换
public class FileUploadController {
/**
* 处理文件上传
* @param filename
* @return
*/
@RequestMapping("fileUploadController")
public Map<String,Object> fileUpload(MultipartFile filename){
System.out.println("取到文件的原始文件名:"+filename.getOriginalFilename());
try {
filename.transferTo(new File("e:/"+filename.getOriginalFilename()));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<String,Object> map=new HashMap<>();
map.put("msg", "ok");
return map;
}
}
启动器
package com.example;
/**
* SpringBoot文件上传启动类
*/
@SpringBootApplication
public class AppFileUpload {
public static void main(String[] args) {
SpringApplication.run(AppFileUpload.class, args);
}
}
对于文件上传,可以对上传文件的大小进行设置,添加SpringBoot的一个配置文件application.properties 。此文件为SpringBoot的默认配置文件,注意此文件必须存放在src/main/resources文件夹下。
application.properties
#设置单个上传文件的大小
spring.http.multipart.maxFileSize=200MB
#设置一次请求上传文件的总容量
spring.http.multipart.maxRequestSize=200MB