1. 程式人生 > >springboot中添加模板引擎freemarker和thymeleaf

springboot中添加模板引擎freemarker和thymeleaf

1.2 nco false spa Coding 目錄 templates -- -name

freemarkder和thymeleaf都是java的模板引擎,這裏只介紹這兩種模板引擎如何在sprongboot中配置:

1. freemarkder

1.1 在pom.xml中添加依賴包

    <!-- 集成freemarker -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </
dependency>

1.2 在配置文件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=

2. thymeleaf

2.1 在pom.xml中添加依賴包

    <!-- 集成thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

2.2 添加配置文件

# ================================================
#                   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=

springboot中添加模板引擎freemarker和thymeleaf