SpringBoot使用thymeleaf模板過程解析
這篇文章主要介紹了SpringBoot使用thymeleaf模板過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.導(dǎo)入依賴
<!-- 添加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)建實(shí)體類
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)建一個(gè)index.html以備后續(xù)使用
5.創(chuàng)建一個(gè)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頁(yè)面
<!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. 瀏覽器測(cè)試
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Springboot @Autowired 無法注入問題
WebappApplication 一定要在包的最外層,否則Spring無法對(duì)所有的類進(jìn)行托管,會(huì)造成@Autowired 無法注入。接下來給大家介紹解決Springboot @Autowired 無法注入問題,感興趣的朋友一起看看吧2018-08-08SpringBoot居然有44種應(yīng)用啟動(dòng)器,你都知道嗎
很多人都不知道SpringBoot應(yīng)用啟動(dòng)器竟然有44個(gè),本文就一起來介紹一下,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-01-01mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說明
這篇文章主要介紹了mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01java常見報(bào)錯(cuò):Array?Out?of?Bounds兩種解決辦法
這篇文章主要給大家介紹了關(guān)于java報(bào)錯(cuò)Array?Out?of?Bounds的兩種解決辦法,Array out of bounds錯(cuò)誤表示你嘗試訪問數(shù)組中不存在的索引,即索引小于零或者大于等于數(shù)組的大小,文中通過代碼將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解
本篇文章主要介紹了java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10使用Spring安全表達(dá)式控制系統(tǒng)功能訪問權(quán)限問題
從spring security 3.0開始已經(jīng)可以使用spring Expression表達(dá)式來控制授權(quán),允許在表達(dá)式中使用復(fù)雜的布爾邏輯來控制訪問的權(quán)限。這篇文章主要介紹了使用Spring安全表達(dá)式控制系統(tǒng)功能訪問權(quán)限,需要的朋友可以參考下2019-11-11java高并發(fā)鎖的3種實(shí)現(xiàn)示例代碼
本篇文章主要介紹了java高并發(fā)鎖的3種實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Java多線程導(dǎo)致CPU占用100%解決及線程池正確關(guān)閉方式
1000萬表數(shù)據(jù)導(dǎo)入內(nèi)存數(shù)據(jù)庫(kù),按分頁(yè)大小10000查詢,多線程,15條線程跑,最后發(fā)現(xiàn)CPU占用100%卡死,那么如何解決,本文就來介紹一下,感興趣的朋友可以了解一下2021-05-05