文章目录
1.前提
- 已经创建好了maven项目,并且导入了
springboot-web
启动器 - maven项目可以正常启动
2.在springboot中整合servelt
2.1通过注解的方式来扫描servelt
在项目的启动类上使用注解
ServeltComponentScan
注解
ServeltComponentScan
注解可以扫描当前包级下的所有的servelt
package cn.liuhao.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.2通过方法的方式来注册servlet组件
package cn.liuhao.project;
import javax.servlet.Servlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.liuhao.project.web.servlet.FirstServlet;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
// 一定要加注解@Bean,这样springboot才会扫描执行这个方法
@Bean
public ServletRegistrationBean getServletRegistraionBean() {
// 注册servlet bean实例,初始化的参数就是所需要注册servlet对象
ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>(new FirstServlet());
// 如果不在servlet上加注解webServlet来设置urlMapping,可以使用方法addUrlMappings设置url mapping
bean.addUrlMappings("/fisrt");
return bean;
}
}
3.在springboot中整合filter(过滤器)
3.1通过注解的方式整合filter
在项目的启动类上使用注解
ServeltComponentScan
注解
ServeltComponentScan
注解可以扫描当前包级下的所有的filter
3.2通过方法注册filter组件
package cn.liuhao.project;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.liuhao.project.web.filter.FirstFilter;
import cn.liuhao.project.web.servlet.FirstServlet;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public ServletRegistrationBean<Servlet> getServletRegistration() {
ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>(new FirstServlet());
bean.addUrlMappings("/first");
return bean;
}
// 使用方法注册filter不需要写注解@ServletComponentScan,但是servlet同样也需要以方法的形式注册
@Bean
public FilterRegistrationBean<Filter> getFilterRegistration() {
// 创建需要注册filter实例
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<Filter>(new FirstFilter());
// 设置拦截url
bean.addUrlPatterns(new String[] { "/*", "*.jsp" });
// 返回值
return bean;
}
}
4.在springboot中整合listener(监听器)
4.1.通过注解方式整合filter
在项目的启动类上使用注解
ServeltComponentScan
注解
ServeltComponentScan
注解可以扫描当前包级下的所有的listener
4.2通过方法注册listener(监听器)
listener
package cn.liuhao.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class Listener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("销毁");
}
}
App启动类
package cn.liuhao;
import javax.servlet.ServletContextListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.liuhao.listener.Listener;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
// 注解@Bean
// 一定不要忘记
@Bean
public ServletListenerRegistrationBean<ServletContextListener> getRegistrationBean() {
// 注册listener bean
return new ServletListenerRegistrationBean<ServletContextListener>(new Listener());
}
}
5.springboot项目访问静态资源
5.1通过在classpath/static目录访问静态资源
在maven项目中我们需要在resoures文件夹下新建static目录
名称必须是static
效果
5.2通过ServletContext目录访问静态资源
在maven项目里,servletContext目录对应着的就是src/main/webapp目录
没有src/main/webapp目录就自己创建
文件夹名称必须要是webapp
效果
6.springboot实现文件上传
6.1前端页面
- 上传文件夹需要更改
form
标签enctype
属性为multipart/form-data
- method必须是post方式
- action直接写Controller定义的接口
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>文件上传</title>
</head>
<body>
<!-- 上传文件夹需要更改enctype属性为multipart/form-data -->
<form action="api/fileupload" method="post" enctype="multipart/form-data">
<!-- input的type属性为file -->
<input type="file" name="filename" value="" />
<p></p>
<input type="submit" value="提交"/>
</form>
</body>
</html>
6.2controller接口与启动类
启动类
package cn.liuhao.fileupload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
controller类
package cn.liuhao.fileupload.controller;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
*
* 注解 @RestController 保留了 @Controller的作用同时指定了当前Controller上的所有返回结果都换为json格式
* 也就是说我们不需要在每一个接口都写 @ResponseBody
*
* @author admin
*
*/
@RestController
public class WebController {
/**
* 文件上传用MultipartFile接口类型接受
*
* 这个参数名称必须和你在html中定义的input的name属性相同,否则接受为null
*
* @param filename
* @return
*/
@RequestMapping("api/fileupload")
public Map<String, Object> apiFileupload(MultipartFile filename) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
if (filename != null) {
// 打印文件的原名称
System.out.println(filename.getOriginalFilename());
filename.transferTo(new File("d:/" + filename.getOriginalFilename()));
result.put("msg", "ok");
} else {
result.put("msg", "failture");
}
return result;
}
}
6.3springboot项目的application属性文件
- springboot项目的application属性文件必须放在resources文件夹下,不能放置在static文件夹内
使用application属性文件指定文件上传大小
文件内容
# 设置spring项目中文件上传最大值的大小,必须带单位MB
spring.servlet.multipart.maxFileSize = 200MB
# 设置上传文件的总大小
spring.servlet.multipart.maxRequestSize=2000MB
注意: 在springboot各个版本的写法不同
- Spring Boot 1.3 版本: multipart.maxFileSize
- Spring Boot 1.4 版本和 1.5 版本: spring.http.multipart.maxFileSize
- Spring Boot 2.0 版本以上: spring.servlet.multipart.maxFileSize
具体详情可以参考下这篇文章:Springboot 文件上传超过限制处理