Apache FreeMarker™是一個模板引擎:一個Java庫,用於根據模板和更改數據生成文本輸出(HTML網頁,電子郵件,配置文件,源代碼等)。模板是用FreeMarker模板語言(FTL)編寫的,這是一種簡單的專用語言(不像PHP這樣的完整編程語言)。一般,使用通用編程語言(如Java)來準備數據(發佈數據庫查詢,進行業務計算)。而後,Apache FreeMarker使用模板顯示準備好的數據。在模板中,您將專一於如何呈現數據,而在模板以外,您將關注於要呈現的數據。html
SpringBoot 官方比較推薦使用 Thymeleaf,因爲本身對 FreeMarker 相對比較熟悉,因此這裏我先介紹一下如何在 SpringBoot 中使用 FreeMarker 當前前端頁面使用。 ——以上內容摘抄自 FreeMarker 官網前端
第一步是在pom.xml 中引入 spring-boot-starter-freemarker 依賴具體代碼以下:java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
複製代碼
第二步是在resources 下 templates 建立test目錄新建 freemarkDemo.ftl 內容以下:git
<h1>${msg}</h1>
複製代碼
第三步是建立訪問 freemarkDemo.ftl 的Controller。github
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg", "SpringBoot With Freemark hello world!");
return "test/helloworld";
}
}
複製代碼
在遊覽器輸入訪問 FreeMarker 頁面的 Controller 的 URL:http://localhost:8080/sbe/hello/test 進行測試,測試結果以下:spring
SpringBoot 使用 FreeMarker 步驟以下:數據庫
具體代碼示例請查看個人 GitHub 倉庫 springbootexamples 中模塊工程名: spring-boot-2.x-freemarker 下進行查看。apache
GitHub:github.com/zhuoqianmin…編程