springboot_3.2_freemark_基礎環境配置
- 一、前言
- 二、環境
- 三、相關資料
- 四、目標
- 五、默認配置項
- 六、構建springboot 3.2項目
- 6.1 pom.xml 內容:
- 6.2 啟動類
- 6.3 添加ftlh模板
- 6.4 controller內容
- 6.5 bootstrap.yml配置
- 七、總結
一、前言
FreeMarker 是一款模板引擎:即一種基于模板和要改變的數據, 并用來生成輸出文本(HTML網頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。
模板編寫為FreeMarker Template Language (FTL)。它是簡單的標簽語言。需要在服務器端組織數據,之后通過編寫FTL模板顯示已經準備好的數據。在模板中,你可以專注于如何展現數據,而在模板之外可以專注于要展示什么數據。
注:從freemarker 2.x 開始模板的默認后綴由 ftl 改為 ftlh(配置類FreeMarkerProperties.DEFAULT_SUFFIX)。
二、環境
- java 21
- springboot 3.2
三、相關資料
- freemark中文管網
四、目標
- sprinboot 3.2 整合freemarker基礎配置;
- 加入freemarker模板;
- 編寫controller將綁定數據輸出到模板中;
- 運行測試頁面;
五、默認配置項
- jar包:spring-boot-autoconfigure-3.2.0.jar
- 對象:org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties
六、構建springboot 3.2項目
項目是通過idea腳手架創建,由于2.x不在維護,建項目時通過服務(https://start.spring.io)只能創建springboot3.x項目,如果想要創建低版本可以使用阿里的腳手架(start.aliyun.com)。springboot3.x只支持java 17 及以上版本。所以建項目前先配置好本地的jdk。
idea腳手架服務更改位置:file/New/New Project/Sping Initializr : Server URL start.string.io ,點擊后面的齒輪標記修改!
6.1 pom.xml 內容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.test</groupId><artifactId>test2312</artifactId><version>0.0.1-SNAPSHOT</version><name>test2312</name><description>test2312</description><properties><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
6.2 啟動類
其中在@SpringBootApplication注解中屏蔽了DataSourceAutoConfiguration.class的加載,目的是避免出現“
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.”
錯誤提示。
因為我們目的只是驗證freemarker,不需要數據庫相關的依賴,所以不需要配置數據源相關配置。而springboot基礎依賴中默認加載了MySQL、Oracle、Mybatis等和數據庫相關的依賴包,結果我們的配置文件中卻沒有添加數據庫相關的配置。
package com.test;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class
})
public class StartApplication {public static void main(String[] args) {SpringApplication.run(StartApplication.class, args);}}
6.3 添加ftlh模板
freemarker2.x開始默認的后綴由ftl改為ftlh了,使用ftl在freemarker3.x中是找不到的。
模板中對spring、list、map等常用的類型,做了綁定和遍歷示例。加了個test目錄是需要與后面的controller的地址一致。
模板位置:resources\templates\test\index.ftlh
<html>
<head><title>Welcome!</title>
</head>
</body><br><h1>Welcome ${user} !</h1><br><b>展示list中的數據:</b><p><ul><#list lst as item><li>${item}</li></#list></ul><ul></ul></p>
<p><b>map遍歷:key值遍歷</b><ul><#assign keys=map?keys/><#list keys as key><li>key=${key}; value=${map["${key}"]};</li></#list></ul><b>map遍歷:直接遍歷</b><ul><#list map?keys as key><li>key=${key}; value=${map["${key}"]};</li></#list></ul><b>map遍歷:直接遍歷值</b><ul><#list map?values as value><li>value=${value};</li></#list></ul>
</p>
</body>
</html>
6.4 controller內容
controller中有兩個方法,分別是返回text內容 和 綁定ftlh模板內容。
package com.test.controller;import lombok.extern.log4j.Log4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.View;import java.util.Arrays;
import java.util.HashMap;@Controller
@RequestMapping("/test")
public class TestController {private Logger logger = LoggerFactory.getLogger(this.getClass());@GetMapping("/hello")@ResponseBodypublic String hello(){logger.info("hello word~~~");return "hello word!";}@GetMapping("/index.html")public String index(Model model){model.addAttribute("user","joy");var lst = Arrays.asList("11","22","33");var lst4empty = new HashMap<>();var map = new HashMap<String,Object>(){{put("k1",11);}{put("k2",22);}{put("k3",33);}};model.addAttribute("lst",lst);model.addAttribute("lst4empty",lst4empty);model.addAttribute("map",map);logger.info("test index ~~~");return "/test/index";}}
6.5 bootstrap.yml配置
使用這個配置可以方便做環境切換,啟動項目時傳入dev這個forfile參數可以讀到dev環境配置。
spring:profiles.active: ${profile:dev}application:name: test2312http:multipart:max-file-size: 2MBmax-request-size: 2MB
---
spring.profiles: dev
spring:freemarker:templateLoaderPath: classpath:/templates/preferFileSystemAccess: true #支持熱加載,默認為true(生產時false)
七、總結
以上就是一個比較基礎的整合過程,需要注意的是通過idea構建時依賴盡量選擇少的依賴,否則容易出現各種錯誤。