背景:
这个demo主要是跟随B站的SpringBoot教学视频进行编写的,大致相同,小部分个人添加,详细内容可参考B站视频:尚硅谷SpringBoot顶尖教程(springboot之idea版spring boot)_哔哩哔哩_bilibili
操作:
先创建一个简单的SpringBoot项目:
可参考:SpringBoot -- idea创建简单的helloWorld项目_DGH2430284817的博客-CSDN博客
pom.xml引入thymeleaf依赖包
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
<!--引入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
创建简单的控制器类(快速开发):
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String , Object> map){
map.put("name","diaoGH");
return "success";
}
}
创建html文件,放到资源目录下的templates文件夹下(没有就新建)
thymeleaf默认的资源目录为classpath:/templates/
可以从项目依赖包中搜索ThymeleafAutoConfiguration , ThymeleafProperties查看对应配置
创建success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span>MyName:</span> <div th:text="${name}"></div>
</body>
</html>
运行项目:
输入地址:http://127.0.0.1:8080/success
效果:
参考代码(码云):
springboot-web-restfulcrud: 简单的springboot项目,简单前段和restful风格的crud