spring boot模板引擎thymleaf用法详解
spring boot模板引擎thymleaf用法詳解
Spring-boot支持FreeMarker、Thymeleaf、jsp、veocity
但是對(duì)freemarker和thymeleaf的支持最好,不推薦使用jsp
使用jsp的弊端
1:項(xiàng)目目錄結(jié)構(gòu)繁瑣
2:頁面不簡潔
3:jsp內(nèi)置錯(cuò)誤頁面不能覆蓋springboot默認(rèn)的錯(cuò)誤頁面
4: 只能打成war不能打成jar
5:內(nèi)置的jetty服務(wù)器不支持jsp
thymeleaf(新一代模版引擎)
優(yōu)點(diǎn):
1:有網(wǎng)無網(wǎng)的情況下模版頁面都可以執(zhí)行,美工的頁面拿來就可以用.
2:相對(duì)jsp減少了額外的標(biāo)簽,頁面也更加簡潔
jar包依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>在application.properties中配置thymleaf
spring.thymeleaf.mode = LEGACYHTML5spring.thymeleaf.mode的默認(rèn)值是HTML5,其實(shí)是一個(gè)很嚴(yán)格的檢查,改為LEGACYHTML5可以得到一個(gè)可能更友好親切的格式要求。
自定義視圖解析
spring.thymeleaf.prefix=classpath:/templates/html/ 不要漏寫 spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false訪問靜態(tài)資源
<img src=”/images/….”> <script src=”/js/….”> <link href=”/css/….”>訪問map中的數(shù)據(jù)
@RequestMapping("/index") public String hello(Model map){//mapMap<String, Object> student= new HashMap<>();student.put("name", "張三豐");map.addAttribute("student",student);return "index"; }在HTML頁面取數(shù)據(jù)
<span th:text="${student.name}"></span> 字符串拼接:<h2 th:text="'姓名:'+${student.name}"></h2> 三元表達(dá)式:<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中的數(shù)據(jù)
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>編號(hào)</td><td>書名</td><td>書價(jià)格</td><td>圖片地址</td></tr><tr th:each="book:${books}"><td>編號(hào)</td><td th:text="${book.bookName}">書名</td><td th:text="${book.bookPrice}">書價(jià)格</td><td th:text="${book.bookUrl}">圖片地址</td></tr> </table>取循環(huán)中的下標(biāo)
<tr th:each="user,userStat : ${list}"> <th th:text="${userStat.index}">狀態(tài)變量:index</th> <th th:text="${userStat.count}">狀態(tài)變量:count</th> <th th:text="${userStat.size}">狀態(tài)變量:size</th> <th th:text="${userStat.current.userName}">狀態(tài)變量:current</th> <th th:text="${userStat.even}">狀態(tài)變量:even****</th> <th th:text="${userStat.odd}">狀態(tài)變量:odd</th> <th th:text="${userStat.first}">狀態(tài)變量:first</th> <th th:text="${userStat.last}">狀態(tài)變量:last</th> </tr> 說明: index:列表狀態(tài)的序號(hào),從0開始; count:列表狀態(tài)的序號(hào),從1開始; size:列表狀態(tài),列表數(shù)據(jù)條數(shù); current:列表狀態(tài),當(dāng)前數(shù)據(jù)對(duì)象 even:列表狀態(tài),是否為奇數(shù),boolean類型 odd:列表狀態(tài),是否為偶數(shù),boolean類型 first:列表狀態(tài),是否為第一條,boolean類型 last:列表狀態(tài),是否為最后一條,boolean類型利用下標(biāo)實(shí)現(xiàn)表格變色
<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類型數(shù)據(jù)渲染成String
<span th:text="${#dates.format(post.postCreate,'yyyy-MM-dd')}">2017年11月8日</span>定義和引用片段
定義
<div th:fragment="copy">© 2014 The Good Thymes Virtual Grocery</div>引用
<body>...<div th:include="footer :: copy"></div> </body>總結(jié)
以上是生活随笔為你收集整理的spring boot模板引擎thymleaf用法详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot使用yaml替代p
- 下一篇: spring boot整合mybatis