spring boot模板引擎thymleaf用法详解
生活随笔
收集整理的這篇文章主要介紹了
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 = LEGACYHTML5spring.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">© 2014 The Good Thymes Virtual Grocery</div>引用
<body>...<div th:include="footer :: copy"></div> </body>總結
以上是生活随笔為你收集整理的spring boot模板引擎thymleaf用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot使用yaml替代p
- 下一篇: spring boot整合mybatis