1. 程式人生 > >SpringBoot整合模板引擎

SpringBoot整合模板引擎

一、SpringBoot整合freemarker:

  1.引入freemarker模板依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>

  2.配置application.properties:

#freemarker 靜態資源配置
#設定ftl檔案路徑
spring.freemarker.template-loader-path=classpath:/templates
#關閉快取,還是重新整理,上線生產環境需要改為true
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose
-request-attributes=true spring.freemarker.expose-session-attributes=true spring.freemarker.request-context-attribute=request spring.freemarker.suffix=.ftl

  3.在resources目錄下建個目錄叫templates,在這個目錄下新建一個以.ftl結尾的檔案,並且將html程式碼複製進去,在controller層返回這個檢視

二、SpringBoot整合thymeleaf

  1.引入thymeleaf模板依賴 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
     <version>1.5.9.RELEASE</version>
</dependency>

   2.配置application.properties:

#thymeleaf靜態資源配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#關閉快取,即時重新整理,上線生產環境需要改為true
spring.thymeleaf.cache=true

  3.在resoureces下新建個目錄templates在裡面編寫html檔案,具體的thymeleaf標籤語法這裡就不做介紹了。