参考视频链接:SpringBoot学习整合_哔哩哔哩_bilibili
一、整合Servlet
1.SpringBoot整合Servlet方式一:通过注解扫描完成Servlet组件的注册
package com.example.demo.files;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author:
* @date:2022/2/10
* @description: SpringBoot整合Servlet方式一
*/
/**
*
* SpringBoot整合Servlet方式一:通过注解扫描完成Servlet组件的注册
* 早期在web.xml中的servlet配置
* <servlet>
* <servelt-name>FirstServlet</servelt-name>
* <servlet-class>com.example.demo.files.FirstServelt</servlet-class>
* </servlet>
* <servlet-mapping>
* <servlet-name>FirstServlet</servlet-name>
* <url-pattern>/first</url-pattern>
* </servlet-mapping>
* */
@WebServlet(name="FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}与之对应的启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //TODO 在SpringBoot启动时会扫描@WebServlet的类,并将该类实例化
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}2.SpringBoot整合Servlet方式二:通过方法完成Servlet组件的注册
package com.example.demo.files;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author:
* @date:2022/2/10
* @description: SpringBoot整合Servlet方式二:通过方法完成Servlet组件的注册
*/
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("second servlet................");
}
}
与之对应的启动类
package com.example.demo;
import com.example.demo.files.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
/**
* @author:
* @date:2022/2/10
* @description: SpringBoot整合Servlet方式二:通过方法完成Servlet组件的注册
*/
@SpringBootApplication
public class DemoApplication2 {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/**
*@param: []
*@return: org.springframework.boot.web.servlet.ServletRegistrationBean
*@description: ServletRegistrationBean是注册servlet的一个对象
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}二、整合Filter
SpringBoot整合Filter方式一:通过注解完成Filter组件的注册
package com.example.demo.files;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
// TODO SpringBoot整合Filter方式
/**
* SpringBoot整合Filter方式一:通过注解完成Filter组件的注册
* 早期的web.xml配置Filter
* <filter>
* <filter-name>FirstFilter</filter-name>
* <filter-class>com.example.demo.files.FirstFilter</filter-class>
* </filter>
* <filter-mapping>
* <filter-name>FirstFilter</filter-name>
* <url-pattern>/first</url-pattern>
* </filter-mapping>
*
*/
//@WebFilter(filterName = "FirstFilter" ,urlPatterns = {"*.do","*.jsp"})//为一个filter配置多个url,对应String数组中的元素
@WebFilter(filterName = "FirstFilter" ,urlPatterns = "/first")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入filter");
}
}
与之对应的启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}SpringBoot整合Filter方式二:通过方法完成Filter组件的注册
package com.example.demo.files;
import javax.servlet.*;
import java.io.IOException;
// TODO SpringBoot整合Filter方式
/**
* SpringBoot整合Filter方式二:通过方法完成Filter组件的注册
*/
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入filter");
}
}与之对应的启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/** SpringBoot整合Filter方式二:通过方法完成Filter组件的注册 */
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}三、整合Listener
SpringBoot整合Listener方式一:通过注解完成Listener组件的注册
package com.example.demo.files;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @author:
* @date:2022/2/10
* @description: SpringBoot整合Listener
*/
/**
*
* SpringBoot整合Listener方式一:通过注解方式完成Listener的注册
*
* 早期web.xml中对Listener的配置
* <listener>
* <listener-class>com.example.demo.files.FirstListener</listener-class>
* </listener>
*
*
* */
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}与之对应的启动类
package com.example.demo;
import com.example.demo.files.SecondFilter;
import com.example.demo.files.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}SpringBoot整合Listener方式二:通过方法完成Listener组件的注册
package com.example.demo.files;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @author:
* @date:2022/2/10
* @description:
*/
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}与之对应的启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/**
* @param:
* @return:
* @description: SpringBoot整合Listener方式二:通过方法完成Listener组件的注册
*/
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {
ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}四、访问静态资源
springboot的静态资源目录必须是在classpath下的static目录
方式一:


方式二:
ServletContext根目录下
加入项目目录中没有webapp目录,就在src/main/webapp手动创建一个webapp,目录名称必须是叫webapp
再创建一个static存放静态文件

五、文件上传

![]()




源码:
package com.example.demo.Control;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author:
* @date:2022/2/10
* @description:
*/
//@Controller +@ResponseBody
@RestController//方法上不用加@ResponseBody
public class controller {
//@GetMapping("/hello")
@RequestMapping(path = "/hello", method = RequestMethod.GET)
public String method() {
return "hello everyone";
}
//@GetMapping("/get") //处理GET请求
@PostMapping("/fileUpload") //处理POST请求
public Map<String, Object> fileUpload(MultipartFile filename) throws IOException {
//打印上传的文件名称
System.out.println(filename.getOriginalFilename());
//filename.transferTo(new File("e:/" + filename.getOriginalFilename()));
Map<String, Object> map = new HashMap<>();
map.put("msg", "ok");
return map;
}
}
六、整合jsp
spring并不推荐用jsp,但是也是支持的

controller层代码:
package com.example.demo.Control;
import com.example.demo.Entity.User;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author:
* @date:2022/2/10
* @description:
*/
//@Controller +@ResponseBody
@RestController//方法上不用加@ResponseBody
public class controller {
//@GetMapping("/hello")
@RequestMapping(path = "/hello", method = RequestMethod.GET)
public String method() {
return "hello everyone";
}
//@GetMapping("/get") //处理GET请求
@PostMapping("/fileUpload") //处理POST请求
public Map<String, Object> fileUpload(MultipartFile filename) throws IOException {
//打印上传的文件名称
System.out.println(filename.getOriginalFilename());
//filename.transferTo(new File("e:/" + filename.getOriginalFilename()));
Map<String, Object> map = new HashMap<>();
map.put("msg", "ok");
return map;
}
/**
*@param: [model]
*@return: java.lang.String
*@description: springboot 整合jsp
*/
@RequestMapping("/showUser")
public String showUser(Model model){
List<User> list = new ArrayList<>();
list.add(new User(1,"张三",20));
list.add(new User(1,"冷少",30));
list.add(new User(1,"王五",30));
//需要一个model对象
model.addAttribute("list", list);
//跳转视图,跳转到userList.jsp
return "userList";
}
}
application.properties

userList.jsp
<%--
Created by IntelliJ IDEA.
User: Admin
Date: 2022/2/10
Time: 17:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl的核心标签库,通过C标签库配合EL表达式,把传过来的数据在页面中做出一个展示--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>hello world Jsp</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.userid}</td>
<td>${user.username}</td>
<td>${user.userage}</td>
</tr>
<c:forEach>
</table>
</body>
</html>
七、整合freemarker
<!--freemarker启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>打开HBuilder打开刚才的Eclipse工程-新建目录resources/templates,注意新建的目录名必须为templates
templates目录下存放*.ftl文件(ftl格式的文件编写和html语法类似)
回到Eclipse工程刷新一下,刚才resources目录下的操作会同步过来。
假如在templates下新加你一个userList.ftl文件里面是html代码,在contorler层最后直接 return userList; 便可实现视图的跳转
八、整合Thymeleaf
Thymeleaf - 使用方法及国际化(超详细)_江南烟雨却痴缠丶-CSDN博客_thymeleaf
thymeleaf资源加载问题(从Controller跳转)_清晨的第一抹阳光a的博客-CSDN博客
在springboot整合thymeleaf模板引擎中@Controller和@RestController不同注解的跳转页面方法 - 海龟先生吖 - 博客园
在springboot整合thymeleaf模板引擎中@Controller和@RestController不同注解的跳转页面方法_╱℡❄&▓的博客-CSDN博客
thymeleaf表达式爆红问题 - 1456710017 - 博客园
IDEA开发springBoot 使用 thymeleaf 模板$表达式报红波浪线解决方案_H_233的博客-CSDN博客
thymeleaf从后台获取值(获取不到值的解决方法)_zhao13106920109的博客-CSDN博客_thymeleaf取不到model值
thymeleaf从后台获取值,分为两种情况
第一,后台是转发情况
第二,后台是重定向情况
引入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<!--JDK版本-->
<java.version>1.8</java.version>
<!--Thymeleaf版本3.0.2RELEASE 3.0的版本检查就没有那么严格了,对于编辑器自动生成不合法的标签也可以通过了-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!--2.0以上就可以了-->
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--freemarker启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--Thymeleaf启动器-->
<dependency>
<!--需要配合spring-boot-starter-web依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
在resources目录下新建templates目录,该目录是安全的,该目录下的内容是不允许外界直接访问的。
如果需要访问,请在application.properties中加入
#spring.resources.static-locations=classpath:/templates/,classpath:/static/ spring.web.resources.static-locations=classpath:/templates/,classpath:/static/
1 Thymeleaf的基本使用
Thymeleaf是通过特定的语法对html的标记做渲染
controller.java
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg","Thymeleaf第一个案例");
return "thymeleaf";
}thymeleaf.html
<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf</title>
</head>
<body>
<h3></h3>
<hr/>
<p>如果你在html中书写thymeleaf表达式,表达式飘红,那么可以在html开头加上suppress ALL</p>
<p></p>
<hr/>
<p>th:text是Thymeleaf的一个标签,它可以向页面中输出一个信息,初次使用thymeleaf标签可以用快捷键导入xmlns:th=""</p>
<span th:text="Hello"></span>
<hr/>
<p>使用thymeleaf的类似于一个el表达式的语法去取数据,传入下面的span标签</p>
<span th:text="${msg}">我将来会被Thymeleaf语法动态的替换掉</span>
</body>
</html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 这个是我Alt+Enter导入的,老师讲的过程中没有导入这句语句。

运行结果

假如在控制台报错
解决方法一:
meta标签是编辑器自动生成的,没有结束标记,自己手动敲上一个即可。

clean一下编辑器,重启程序。
解决方法二:
pom引用
为什么成这样了呢?
控制台也没报错,返回页面就一个字符串。
答:不要让controller.java中出现@ResponseBody,否则无法跳转,会导致直接把字符串返回到页面上
2 Thymeleaf字符串操作
3 日期转换操作
4 条件判断
5 迭代遍历
6 获取作用域对象中的数据
7 URL表达式
九、SpringBoot热部署
方法一-pom.xml中引入SpringLoader
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--<packaging>war</packaging>-->
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<!--JDK版本-->
<java.version>1.8</java.version>
<!--Thymeleaf版本3.0.2RELEASE 3.0的版本检查就没有那么严格了,对于编辑器自动生成不合法的标签也可以通过了-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!--2.0以上就可以了-->
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<profiles>
<!--开发环境-设置为默认-->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>dev</environment>
</properties>
</profile>
<!--生产环境-关闭-->
<profile>
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Thymeleaf启动器-->
<dependency>
<!--需要配合spring-boot-starter-web依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<!--剔除和现有依赖有冲突的包-->
<exclusions>
<exclusion>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<resources>
<!--排除配置文件-->
<resource>
<directory>src/main/resources</directory>
<!--先排除所有配置文件-->
<excludes>
<exclude>application*.yml</exclude>
</excludes>
</resource>
<!--根据激活条件引入包所需的配置和文件-->
<resource>
<directory>src/main/resources</directory>
<!--引入所需环境的配置文件-->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<!--根据maven选择环境导入配置文件,取前面environment标签中的字段-->
<include>application-${environment}.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<!--springloader插件(springboot热部署)-->
<build>
<plugins>
<plugin>
<groupID>org.springframework.boot</groupID>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
缺点:java代码做部署处理,面对页面无能为力,对于修改.html页面文件不会自动热部署
注意:这种方式的缺点是springloader热部署程序是在系统后台以进程形式运行,当项目停以后,进程需要手动关闭
方法二-springboot项目中使用jar包形式去运行
下载springloaded-1.2.5RELEASE.jar放到项目中新建的lib目录下
右键项目-Run Configuration-arguments-VM arguments-配置上参数:
-javaagent:.\lib\springloaded-1.2.5RELEASE.jar -noverify
这样插件的启动和关闭相对于第一种方法比较容器一些,并且没有端口抢占现象。
方法三-springboot热部署通过dev-tools
springloader是热部署方式:对于修改.html页面文件不会自动热部署
devtools是采用重新部署方式:
具体表现之一就是我在controller层稍微修改一处地方,控制台的打印信息就重新开始打印一遍项目的启动信息
pom文件引入dev-tools依赖即可
<!--dev-tools依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--有其他项目继承的时候,当前的依赖不向下传递的-->
<optional>true</optional>
</dependency>
