10.SpringBoot文件上传


SpringBoot整合Thymeleaf

1.创建Thymeleaf的入门项目

  1. maven构建简单项目

  2. 修改pom文件添加thymeleaf的坐标

    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
  1. 创建存放视图的目录:src/main/resources/templates

templates:该目录是安全的,意味着该目录下的内容是不允许外界直接访问的(不能通过url直接访问)。

2.Thymeleaf的基本使用

1.Thymeleaf的特点

Thymeleaf是通过他特定的语法对html的标记做渲染。

2.编写controller
/**
 * Thymeleaf 入门案例
 */
@Controller
public class DemoController {
    @RequestMapping("/show")
    public String showInfo(Model model) {
        model.addAttribute("msg","Thymeleaf 第一个案例");
        return "index";
    }
}
3.编写页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf 入门</title>
</head>
<body>
    <span th:text="Hello"></span>
    <hr/>
    <span th:text="${msg}"></span>
</body>
</html>
4.编写启动器
5.运行

有可能会出现没有html结束标记的异常:
我的项目的Springboot的版本是2.1.2所以没有出现此异常

  1. 加上结束标记即可。让html的标记按照严谨的语法编写。
  2. 修改导入的Thymeleaf的jar包的版本到 3.0以上
<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  </properties>


原文链接:https://www.cnblogs.com/net-safe/p/13529126.html