03.Spring Boot 实战~SpringBoot 整合Thymeleaf
本文是上一篇文章的后续,详情点击该链接
SpringBoot 访问静态资源
在 SpringBoot 项目中没有我们之前常规 web 开发的 WebContent(WebApp),它只有 src 目录。在 src/main/resources 下面有两个文件夹, static 和 templates。 SpringBoot 默认在 static 目录中存放静态页面,而 templates 中放动态页面。
static 目录
SpringBoot 通过 classpath/static 目录访问静态资源。注意存放静态资源的目录名称必须 是 static。
我们在静态目录里创建一个HTML文件,然后直接访问即可。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>hello world</h2>
</body>
</html>
SpringBoot 访问静态资源的位置
classpath:/META‐INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
自定义静态资源位置
这个时候我们发现报了一个404错误,为什么呢?因为SpringBoot只会访问我们上面列举的四个静态位置,除此之外不会去其他地方查找。
那么我们如果把我们的静态资源放到自定义目录当中,那么我们肯定要修改配置文件,来修改默认配置。
spring.resources.static-locations=classpath:/MyStaticDire/
SpringBoot 文件上传
前台
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/FileLoad" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类必须要有这个注解!
@SpringBootApplication
public class FileLoadSpringBoot {
public static void main(String[] args) {
//必须要通过SpringApplication下的静态方法run
//第一个参数是启动类的class,第二个参数是main方法里的args
SpringApplication.run(FileLoadSpringBoot.class,args);
}
}
Controller
@Controller
public class MyController {
@PostMapping("/FileLoad")
@ResponseBody
public String FileLoad(MultipartFile file) throws IOException {
//如果file不等于空说明数据传过来了
if(file!=null){
file.transferTo(new File("C:\\Users\\36961\\Desktop\\Test"));
return "OK";
}
//file是空的话,那么就说明上传失败
return "error";
}
}
Thymeleaf
Thymeleaf 的主要目标是将优雅的自然模板带到开发工作流程中,并将 HTML 在浏览器 中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf 能够处理 HTML,XML,JavaScript,CSS 甚至纯文本。
长期以来,jsp 在视图领域有非常重要的地位,随着时间的变迁,出现了一位新的挑战 者:Thymeleaf,Thymeleaf 是原生的,不依赖于标签库.它能够在接受原始 HTML 的地方进行编 辑和渲染.因为它没有与Servelet规范耦合,因此Thymeleaf模板能进入jsp所无法涉足的领域。
Thymeleaf在Spring Boot项目中放入到resources/templates中。这个文件夹中的内容是无法通过浏览器URL直接访问的(和WEB-INF效果一样),所有Thymeleaf页面必须先走控制器。
添加Thymeleaf启动器
<!-- 添加Thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果项目用的是分布式架构,前后端分离的话,Thymeleaf就没什么技术特点了,因为前后端分离各尽其职,后台只需要提供接口即可。但如果是单体结构的项目当中,对于视图层技术而言,Thymeleaf的好处远远大于JSP。
Thymeleaf启动器依赖
<!-- 添加Thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
我们先来段代码感受一下
创建项目
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootAndThymeleaf {
public static void main(String[] args) {
SpringApplication.run(SpringBootAndThymeleaf.class,args);
}
}
Controller
@Controller
public class MyController {
@RequestMapping("/show")
public String showPage(Model model){
//发送数据到后台
model.addAttribute("msg","hello Thymeleaf");
return "index";
}
}
视图层
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${msg}"></span>
</body>
</html>