日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot模板引擎thymleaf用法详解

發布時間:2025/1/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot模板引擎thymleaf用法详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring boot模板引擎thymleaf用法詳解

Spring-boot支持FreeMarker、Thymeleaf、jsp、veocity
但是對freemarker和thymeleaf的支持最好,不推薦使用jsp

使用jsp的弊端

1:項目目錄結構繁瑣

2:頁面不簡潔

3:jsp內置錯誤頁面不能覆蓋springboot默認的錯誤頁面

4: 只能打成war不能打成jar

5:內置的jetty服務器不支持jsp

thymeleaf(新一代模版引擎)

優點:

1:有網無網的情況下模版頁面都可以執行,美工的頁面拿來就可以用.

2:相對jsp減少了額外的標簽,頁面也更加簡潔

jar包依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

在application.properties中配置thymleaf

spring.thymeleaf.mode = LEGACYHTML5

spring.thymeleaf.mode的默認值是HTML5,其實是一個很嚴格的檢查,改為LEGACYHTML5可以得到一個可能更友好親切的格式要求。

自定義視圖解析

spring.thymeleaf.prefix=classpath:/templates/html/ 不要漏寫 spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false

訪問靜態資源

<img src=”/images/….”> <script src=”/js/….”> <link href=”/css/….”>

訪問map中的數據

@RequestMapping("/index") public String hello(Model map){//mapMap<String, Object> student= new HashMap<>();student.put("name", "張三豐");map.addAttribute("student",student);return "index"; }

在HTML頁面取數據

<span th:text="${student.name}"></span> 字符串拼接:<h2 th:text="'姓名:'+${student.name}"></h2> 三元表達式:<input th:value="${age gt 30 ? '中年':'年輕'}"/> gt:great than(大于) ge:great equal(大于等于) eq:equal(等于) lt:less than(小于) le:less equal(小于等于) ne:not equal(不等于)

訪問pojo中的屬性

Book book = new Book("辟邪劍譜",199.99f,"http://img3m6.ddimg.cn/15/16/23326296-1_w_2.jpg"); map.addAttribute("book",book);

在HTML頁面取值

<img th:src="${book.bookUrl}"/><span th:text="${book.bookName}"/>

取list中的數據

List<Book> books = new ArrayList<Book>(); for (int i = 0; i < 10; i++) {Book b = new Book("book"+i, 100f, "http://www.wendaoxueyuan.com/images/"+i+".jpg");books.add(b); } map.addAttribute("books",books);

HTML頁面取值

<table border="1px" cellspacing="0px" cellspadding="0px" width="100%"><tr><td>編號</td><td>書名</td><td>書價格</td><td>圖片地址</td></tr><tr th:each="book:${books}"><td>編號</td><td th:text="${book.bookName}">書名</td><td th:text="${book.bookPrice}">書價格</td><td th:text="${book.bookUrl}">圖片地址</td></tr> </table>

取循環中的下標

<tr th:each="user,userStat : ${list}"> <th th:text="${userStat.index}">狀態變量:index</th> <th th:text="${userStat.count}">狀態變量:count</th> <th th:text="${userStat.size}">狀態變量:size</th> <th th:text="${userStat.current.userName}">狀態變量:current</th> <th th:text="${userStat.even}">狀態變量:even****</th> <th th:text="${userStat.odd}">狀態變量:odd</th> <th th:text="${userStat.first}">狀態變量:first</th> <th th:text="${userStat.last}">狀態變量:last</th> </tr> 說明: index:列表狀態的序號,從0開始; count:列表狀態的序號,從1開始; size:列表狀態,列表數據條數; current:列表狀態,當前數據對象 even:列表狀態,是否為奇數,boolean類型 odd:列表狀態,是否為偶數,boolean類型 first:列表狀態,是否為第一條,boolean類型 last:列表狀態,是否為最后一條,boolean類型

利用下標實現表格變色

<tr th:class="${pcStatus.even?'red':'blue'}">

if判斷

<h1><b th:text="${name}"></b>:<span th:if="${age gt 30}">中年</span><span th:unless="${age gt 30}">年輕</span> </h1>

將pojo中的Date類型數據渲染成String

<span th:text="${#dates.format(post.postCreate,'yyyy-MM-dd')}">2017年11月8日</span>

定義和引用片段

定義

<div th:fragment="copy">&copy; 2014 The Good Thymes Virtual Grocery</div>

引用

<body>...<div th:include="footer :: copy"></div> </body>

總結

以上是生活随笔為你收集整理的spring boot模板引擎thymleaf用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。