SpringBoot使用thymeleaf模板過程解析
這篇文章主要介紹了SpringBoot使用thymeleaf模板過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1.導入依賴
<!-- 添加thymeleaf模版的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.application.yml文件中新增thymeleaf配置
###配置thymeleaf spring: thymeleaf: cache: false
3.創(chuàng)建實體類
public class Student {
private Integer stu_id;
private String stu_name;
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public Student(Integer stu_id, String stu_name) {
this.stu_id = stu_id;
this.stu_name = stu_name;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
}
4.在src/main/resource文件夾下創(chuàng)建templates文件夾
并創(chuàng)建一個index.html以備后續(xù)使用

5.創(chuàng)建一個ThyController類
@Controller
@RequestMapping("/thyController")
public class ThyController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
List<Student> list=new ArrayList<>();
Student stu1=new Student(1,"張三");
Student stu2=new Student(2,"李四");
Student stu3=new Student(3,"王五");
list.add(stu1);
list.add(stu2);
list.add(stu3);
model.addAttribute("stuList",list);
return "index";
}
}
6.hello.html頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>ss</title>
</head>
<body>
<ul th:each="stu:${stuList}">
<li><span th:text="${stu.stu_id}"></span><span th:text="${stu.stu_name}"></span></li>
</ul>
</body>
</html>
7. 瀏覽器測試

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決Springboot @Autowired 無法注入問題
WebappApplication 一定要在包的最外層,否則Spring無法對所有的類進行托管,會造成@Autowired 無法注入。接下來給大家介紹解決Springboot @Autowired 無法注入問題,感興趣的朋友一起看看吧2018-08-08
java常見報錯:Array?Out?of?Bounds兩種解決辦法
這篇文章主要給大家介紹了關于java報錯Array?Out?of?Bounds的兩種解決辦法,Array out of bounds錯誤表示你嘗試訪問數組中不存在的索引,即索引小于零或者大于等于數組的大小,文中通過代碼將解決的辦法介紹的非常詳細,需要的朋友可以參考下2024-08-08
Java多線程導致CPU占用100%解決及線程池正確關閉方式
1000萬表數據導入內存數據庫,按分頁大小10000查詢,多線程,15條線程跑,最后發(fā)現CPU占用100%卡死,那么如何解決,本文就來介紹一下,感興趣的朋友可以了解一下2021-05-05

