前面幾篇介紹了返回json資料提供良好的RESTful api,下面我們介紹如何把處理完的資料渲染到頁面上。
Spring Boot 使用模板引擎
Spring Boot 推薦使用Thymeleaf、FreeMarker、Velocity、Groovy、Mustache等模板引擎。不建議使用JSP。
Spring Boot 對以上幾種引擎提供了良好的預設配置,預設 src/main/resources/templates 目錄為以上模板引擎的配置路徑。
一、Spring Boot 中使用Thymeleaf模板引擎
簡介:Thymeleaf 是類似於Velocity、FreeMarker 的模板引擎,可用於Web與非Web環境中的應用開發,並且可以完全替代JSP 。
1、pom.xml 新增依賴
<!-- thymeleaf 模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、寫controller
/** * @author sam * @since 2017/7/16 */ @Controller public class HomeController { @RequestMapping("/home") public String home(ModelMap modelMap) { modelMap.put("name", "Magical Sam"); List<String> list = new ArrayList<>(); list.add("sam a"); list.add("sam b"); list.add("sam c"); list.add("sam d"); modelMap.put("list", list); return "home"; } }
3、寫html程式碼,其中th:text="${name}" 為thymeleaf的語法,具體可參考:Thymeleaf 官方文件
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Home</title> </head> <body> <span th:text="${name}"></span> <ul> <li th:each="item : ${list}" th:text="${item}"></li> </ul> </body> </html>
#啟動應用,訪問:http://localhost:8080/home ,可以得到相應結果。
如需修改thymeleaf 的預設配置,可以在application.properties中新增:
# ================================================ # Thymeleaf配置 # ================================================ # 是否启用thymeleaf模板解析 spring.thymeleaf.enabled=true # 是否开启模板缓存(建议:开发环境下设置为false,生产环境设置为true) spring.thymeleaf.cache=false # Check that the templates location exists. spring.thymeleaf.check-template-location=true # 模板的媒体类型设置,默认为text/html spring.thymeleaf.content-type=text/html # 模板的编码设置,默认UTF-8 spring.thymeleaf.encoding=UTF-8 # 设置可以被解析的视图,以逗号,分隔 #spring.thymeleaf.view-names= # 排除不需要被解析视图,以逗号,分隔 #spring.thymeleaf.excluded-view-names= # 模板模式设置,默认为HTML5 #spring.thymeleaf.mode=HTML5 # 前缀设置,SpringBoot默认模板放置在classpath:/template/目录下 spring.thymeleaf.prefix=classpath:/templates/ # 后缀设置,默认为.html spring.thymeleaf.suffix=.html # 模板在模板链中被解析的顺序 #spring.thymeleaf.template-resolver-order=
二、Spring Boot 中使用FreeMarker模板引擎
#1、pom.xml 添加依賴
<!-- freemarker 模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、寫controller
同上。
3、templates 下新建home.ftl檔案編寫html程式碼,freemarker語法可參考:FreeMarker 官方文件
home.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span>${name}</span> <ul> <#list list as item > <li>${item}</li> </#list> </ul> </body> </html>
啟動應用,造訪:http://localhost:8080/home ,可以得到對應結果。
如需修改 freemarker 的預設配置,可以在application.properties中新增:
# ================================================ # FreeMarker配置 # ================================================ # 是否开启模板缓存 spring.freemarker.cache=true # 编码格式 spring.freemarker.charset=UTF-8 # 模板的媒体类型设置 spring.freemarker.content-type=text/html # 前缀设置 默认为 "" spring.freemarker.prefix= # 后缀设置 默认为 .ftl spring.freemarker.suffix=.ftl #spring.freemarker.allow-request-override=false #spring.freemarker.check-template-location=true #spring.freemarker.expose-request-attributes=false #spring.freemarker.expose-session-attributes=false #spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.request-context-attribute= #spring.freemarker.template-loader-path=classpath:/templates/ #spring.freemarker.view-names=
以上是關於Spring Boot系列web開發之Thymeleaf以及FreeMarker模板引擎詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器