基於springboot的Maven項目開發步驟(4)

這篇我們說一下springboot整合模板引擎。一般項目裏是需要用頁面來展示數據的。springboot裏可以整合freemarker模板,還可以整合thymeleaf模板。

【整合freemarker模板】步驟:

1、打開pom.xml文件,添加如下依賴:同樣添加到<dependencies>標籤內。

              <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

2、在src/main/resources裏新建一個文件夾,這個文件夾用於放置freemarker頁面模板(頁面文件後綴是ftl)。文件夾名稱自定義,我這裏定義爲templates。

image.png

3、打開application.properties文件,添加以下配置:如圖


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


image.png

4、接下來我們在templates文件夾下新建一個ftl文件,ftl文件裏的內容與html文件的寫法一樣。

image.png

代碼如下:

image.png

我們在頁面裏添加數據:

image.png

5、在controller類裏添加以下方法:該方法跳轉都test.ftl頁面上,並把數據傳輸出去。

image.png

6、打開瀏覽器,輸入http://localhost:8080/test/toftl,回車即可看到效果:

image.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章