天天看點

詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六

上一篇: 官方推薦!2種方式SpringBoot整合JSP | 帶你讀《SpringBoot實戰教程》之十五 下一篇: SpringBoot整合Thymeleaf | 帶你讀《SpringBoot實戰教程》之十七 本文來自于千鋒教育在阿裡雲開發者社群學習中心上線課程《SpringBoot實戰教程》,主講人楊紅豔, 點選檢視視訊内容

SpringBoot整合Freemarker

在工程中添加相關依賴:

<!-- springboot不建議使用jsp,使用模闆引擎,
      比如themleaf,velocity,freemarker
      整合freemarker -->
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>           

首先建立相關檔案夾,把模闆放入檔案夾中

詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六

建立模闆,模闆相當于最終顯示的頁面:

詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六
詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六

編寫controller:

詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六

啟動完成,通路結果為:

詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六

顯然大家可以看到沒有做全局配置,模闆也取到了資料,說明SpringBoot預設查找.ftl

如果沒有放到templates下,就要做相應的全局配置。

全局配置有這些:

#springboot整合freemarker
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates           

如果你采用的是預設配置,全局配置你是不需要寫的。