關於Thymeleaf的優勢 : 若是隻想替換掉jsp展示形式會選擇Thymeleaf模板html
代碼演示
相關pom依賴java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
application.properties文件通常不採用默認後綴,改爲application.yml
Spring Boot官方文檔建議在開發時將緩存關閉,那就在application.properties文件中加入spring.thymeleaf.cache=false
默認是true
application.ymlweb
server: port: 80 servlet: context-path: / spring: thymeleaf: cache: false
Userspring
package com.myy.springboot01.entity; import lombok.Data; /** * @author 熊貝貝 * @site www.myy.com * @company * @create 2019-12-27 20:29 */ @Data public class User { private String uid; private String uname; private String pwd; public User(String uid, String uname, String pwd) { this.uid = uid; this.uname = uname; this.pwd = pwd; } public User() { } }
UserController瀏覽器
package com.myy.springboot01.controller; import com.myy.springboot01.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.List; /** * @author 熊貝貝 * @site www.myy.com * @company * @create 2019-12-27 20:20 * * 介紹Thymeleaf模板的應用 */ @Controller @RequestMapping("/thymeleaf") public class UserController { @RequestMapping("/list") public ModelAndView list(){ System.out.println("這是list"); ModelAndView mv = new ModelAndView(); mv.addObject("title","員工列表"); List<User> list = new ArrayList<>(); list.add(new User("1","張三","123")); list.add(new User("2","李四","12345")); list.add(new User("3","王五","123456")); mv.addObject("users",list); // 爲何須要講解HTML轉義? mv.addObject("msg","<span style='color:red'>優秀員工是張三</span>"); mv.setViewName("list"); return mv; } }
list.html
HTML頁面提示標籤緩存
<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf知識點</title> </head> <body> <table border="1" cellpadding="0" cellspacing="0" width="60%"> <caption th:text="${title}"></caption> <tr> <th>ID</th> <th>名字</th> <th>密碼</th> </tr> <tr th:each="u : ${users}"> <td th:text="${u.uid}"></td> <td th:text="${u.uname}"></td> <td th:text="${u.pwd}"></td> </tr> </table> <p th:utext="${msg}"></p> </body> </html>
運行瀏覽器顯示:
springboot
性能優化會選擇Freemarker模板
學習網站:http://freemarker.foofun.cn/
導入pom依賴性能優化
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--能夠不加,可是作項目的時候可能會用--> <resources> <!--解決mybatis-generator-maven-plugin運行時沒有將XxxMapper.xml文件放入target文件夾的問題--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!--freemarker模板也讀取須要註釋標紅地方--> <resource> <directory>src/main/resources</directory> <includes> <!--<include>*.properties</include>--> <!--<include>*.xml</include>--> <!--<include>*.yml</include>--> </includes> </resource> </resources>
application.yml文件的默認配置markdown
server: port: 80 servlet: context-path: / spring: thymeleaf: cache: false freemarker: # 設置模板後綴名 suffix: .ftl # 設置文檔類型 content-type: text/html # 設置頁面編碼格式 charset: UTF-8 # 設置頁面緩存 cache: false # 設置ftl文件路徑,默認是/templates,爲演示效果添加role template-loader-path: classpath:/templates/role mvc: static-path-pattern: /static/**
注意層級關係mybatis
至關於:
springmvc.xml internalResouresviewResolver prefix classpath:/templates/role subfix .ftl
配置freemarker。也可不配置,則建立ftl文件時改後綴名
Role
package com.myy.springboot01.entity; import lombok.Data; /** * @author 熊貝貝 * @site www.myy.com * @company * @create 2019-12-27 20:29 */ @Data public class Role { private String rid; private String rname; public Role(String rid, String rname) { this.rid = rid; this.rname = rname; } public Role() { } }
RoleController
package com.myy.springboot01.controller; import com.myy.springboot01.entity.Role; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.List; /** * @author 熊貝貝 * @site www.myy.com * @company * @create 2019-12-27 20:20 * * 介紹Thymeleaf模板的應用 */ @Controller @RequestMapping("/freemarker") public class RoleController { @RequestMapping("/list") public ModelAndView list(){ System.out.println("這是list"); ModelAndView mv = new ModelAndView(); mv.setViewName("list"); mv.addObject("name","張三"); mv.addObject("sex","gay"); List<Role> list = new ArrayList<>(); list.add(new Role("1","超級會員")); list.add(new Role("2","會員")); list.add(new Role("3","普通用戶")); mv.addObject("roles",list); return mv; } }
list.ftl
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Freemarker模板知識點</title> </head> <body> <h2>取值</h2> <h3>提供默認值</h3> welcome 【${name!'未知'}】 to freemarker! <h3>exists用在邏輯判斷</h3> <#if name?exists> ${name} </#if> <h2>條件</h2> <#if sex=='girl'> 女 <#elseif sex=='boy'> 男 <#else> 保密 </#if> <h2>循環</h2> <table border="1px" width="600px"> <thead> <tr> <td>ID</td> <td>角色名</td> <td>描述</td> </tr> </thead> <tbody> <#list roles as role> <tr> <td>${role.rid}</td> <td>${role.rname}</td> </tr> </#list> </tbody> </table> <h2>include</h2> <#include 'foot.ftl'> <h2>局部變量(assign)/全局變量(global)</h2> <#--獲取全路徑名${pageContext.request.contextpath}--> <#assign ctx1> ${springMacroRequestContext.contextPath} </#assign> <#global ctx2> ${springMacroRequestContext.contextPath} </#global> ${ctx1}和${ctx2} </body> </html>
foot.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>底部版權信息</title> </head> <body> xxxx 2019myy版權全部 </body> </html>
瀏覽器運行結果顯示:
若是頁面沒有效果,有多是pom依賴缺損壞。刪掉該jar包的中央倉庫,從新下載便可。
注意點:
一、application.yml中能夠配置模板存放位置的根路徑、以及靜態資源文件存放位置的根路徑
二、${springMacroRequestContext.contextPath}:SpringBoot中獲取項目名
三、不推薦使用全局變量。即使它們屬於不一樣的命名空間, 全局變量也被全部模板共享,由於它們是被 import進來的。
四、freemarker模板也能夠像jsp那樣設置根路徑
common.html
<div th:fragment="h1"> 第一部份內容 </div> <div th:fragment="h2"> 第二部份內容 </div> <div th:fragment="h3"> 第三部份內容 </div>
list.html
<!--thymeleaf包含公共區域的方案--> <!--<div th:include="common.html"></div>--> <div th:include="common :: h1"></div> <div th:include="common :: h3"></div>
瀏覽器運行