# Thymeleaf ###### tags: `Java` `Spring Boot` [TOC] 由 Spring Boot官方強力推薦,因jsp的缺點多逐漸遭人們棄用,為此選用第三方的模板引擎。目前市面上開源的第三方模板引擎 Thymeleaf、FreeMaker、Velocity 等受眾較廣。 ## 模板引擎 可以生成特定格式的文件,如html。 (此處指Web的模板引擎) - 讓網站實現介面和資料分離 - 提高開發效率 - 提高頁面、程式碼的複用性 ## pom.xml引入依賴 以下以官方Thymeleaf 3.0作為示範 ```=java <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.0.RELEASE</version> </dependency> ``` 此為spring 4的依賴 ```=java <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.0.RELEASE</version> </dependency> ``` ## 引入頁面片段 首先必須在html檔 `<html>` 標籤內加入 `<html xmlns:th="http://www.thymeleaf.org"></html>` (以 header.html 為假設) ```=htmlembedded <html xmlns:th="http://www.thymeleaf.org"> <header> <h1>I'm header</h1> </header> </html> ``` ### th:insert 把片段插入所屬標籤中。 ```=htmlembedded <div th:insert="路徑/header"></div> ``` header此檔案內的程式碼片段會直接加入此div標籤內,如下 ```=htmlembedded <div> <header> <h1>I'm header</h1> </header> </div> ``` ### th:replace 把片段取代所屬標籤。 ```=htmlembedded <div th:replace="路徑/header"></div> ``` 此div標籤會被header此檔案內的程式碼片段取代,如下 ```htmlembedded <header> <h1>I'm header</h1> </header> ``` ### th:include 把片段內容插入所屬標籤中(3.0開始不建議使用)。 ```htmlembedded <div th:include="路徑/header"></div> ``` 會加入header此檔案內程式碼片段的“內容”,如下 ```=htmlembedded <div> <h1>I'm header</h1> </div> ``` ## 角色權限相關語法 ```=html <p sec:authorize="isAuthenticated()"> <span>User is Logged In.(登入的話顯示)</span> <span sec:authentication="principal.username">username</span> </p> <p sec:authorize="!isAuthenticated()"> <span>User is Logged Out.(未登入的話顯示)</span> </p> <p sec:authorize="hasAuthority('admin')"> <span>如果擁有admin的權限即顯示</span> </p> <p sec:authorize="hasAnyAuthority('user','admin')"> <span>如果擁有任一權限(user或admin)即顯示</span> </p> <p sec:authorize="hasRole('ROLE_SUPERUSER')"> <span>This will only be displayed if authenticated user has role ROLE_SUPERUSER.</span> </p> <p sec:authorize="hasRole('ROLE_ADMIN')"> <span>This will only be displayed if authenticated user has role ROLE_ADMIN.</span> </p> <p sec:authorize="hasRole('ROLE_USER')"> <span>This will only be displayed if authenticated user has role ROLE_USER.</span> </p> <p th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}"> <span>This will only be displayed if authenticated user has role ROLE_ADMIN.</span> </p> <p th:if="${#authorization.expr('hasRole(''ROLE_ADMIN'')')}"> <span>This will only be displayed if authenticated user has role ROLE_ADMIN.</span> </p> ``` ### 檢查角色權限 此方法不使用sec名稱空間,完全不需要thymeleaf-extras-springsecurity4依賴項。 ```=html <p th:if="${#request.isUserInRole('SUPERUSER')}"> <span>This will only be displayed if authenticated user has role ROLE_SUPERUSER.</span> </p> ``` --- 參考資料 * [Thymeleaf 3 ten-minute migration guide](https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) * [Thymeleaf從入門到精通](https://iter01.com/518210.html)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up