Freemark基礎學習
1、freemark基礎
1.1 基礎語法種類
-
註釋:<#-- 內容 -->
<#--hello ${name}!-->
-
插值表達式:${變量}
-
FTL:和HTMl類似,名字前加#予以區分,freemark會解析標籤中的邏輯和表達式。
1.2 list指令
<#list stus as stu>
// 序號表示方法
${stu_index+1}
1.3 遍歷map數據
// 方式一
${stuMap['stu1'].name}
${stuMap['stu1'].age}
// 方式二
${stuMap.stu1.name}
${stuMap.stu1.age}
// 遍歷map
<#list stuMap?keys ad k>
姓名:${stuMap[k].name}
年齡:${stuMap[k].age}
1.4 if 指令
<td <#if stu.name == 'smilevers'>style="background: cornflowerblue;"</#if> >
1.5 邏輯表達式
支持常見的邏輯表達式
- > :大於號,用gt代替,不然解析失敗
- >=:大於等於,用gte
- < :小於,用lt
- <=:小於等於,用lte
注意:= 和 != 可以用於字符串,數值和日期的比較,但是兩邊必須是相同的類型。其他運算符可以用於數字和日期的比較,但不能用於字符串。
1.6 空值處理
-
空值:
// ??來判斷是否空值,一般在遍歷之前判斷 <#if stus ??> </#if>
-
缺省值:!
// 表示:如果這個值存在就顯示,不然就顯示‘’字符串,類似三元表達式 ${(stu.bestFriend.name)!''}
1.7 內建函數
-
判斷集合的大小
${集合名?szie}
-
日期格式化
// 顯示年月日 ${today?date} // 顯示時分秒 ${today?time} // 顯示日期+時間 ${today?datetime} // 自定義格式化 ${today?string("yyyy年MM月")}
-
內建函數c
將數值型轉成字符串${number?c}
-
將json字符串轉成對象
<#assign text="json字符串"/> <#assign data=text?eval> ${data.name}
2、freemark使用
2.1 添加依賴
<!--freemark依賴*-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.2 配置yml
// spring.data下
freemarker:
cache: false #關閉模板引擎
settings:
template_update_delay: 0 #檢查模板更新延遲時間,設置爲0立即檢查,如果時間大於0 有緩存不方便測試
2.3 靜態化流程
ftl文件 + 模板數據 => 靜態文件
/**
* 頁面靜態化演示
* @Author: smile
* @Date: 2020/2/24
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class FreemarkTest {
/**
* 模板文件 + 模板數據 => 靜態文件string
* @throws IOException
* @throws TemplateException
*/
@Test
public void testGenerateHtml() throws IOException, TemplateException {
// 獲取freemark的配置類
Configuration configuration = new Configuration(Configuration.getVersion());
// 獲取classpath的路徑
String path = this.getClass().getResource("/").getPath();
// 定義模板路徑
configuration.setDirectoryForTemplateLoading(new File(path+"/templates/"));
// 獲取模板對象
Template template = configuration.getTemplate("test1.ftl");
//定義數據模型
Map map = this.getMap();
// 靜態化,模板對象轉字符串
String templateIntoString = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(templateIntoString);
// 輸出到硬盤,IOUtils 輸入輸出流工具(開發中這一步不一定需要)
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = IOUtils.toInputStream(templateIntoString, "utf-8");
outputStream = new FileOutputStream(new File("D:/test1.html"));
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
outputStream.close();
}
}
/**
* 模板文件string + 模板數據 => 靜態文件string
* @throws IOException
* @throws TemplateException
*/
@Test
public void testGenerateHtmlToString() throws IOException, TemplateException {
// 獲取freemark的配置類
Configuration configuration = new Configuration(Configuration.getVersion());
String templateString = "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>hello world</title>\n" +
"\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"hello ${name}!\n" +
"\n" +
"</body>\n" +
"</html>";
// 使用模板加載器
StringTemplateLoader templateLoader = new StringTemplateLoader();
templateLoader.putTemplate("template", templateString);
// 在配置類中設置模板加載器
configuration.setTemplateLoader(templateLoader);
//獲取模板對象
Template template = configuration.getTemplate("template", "utf-8");
//定義數據模型
Map map = this.getMap();
// 靜態化,模板對象轉字符串
String templateIntoString = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(templateIntoString);
// 輸出到硬盤,IOUtils 輸入輸出流工具(開發中這一步不一定需要)
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = IOUtils.toInputStream(templateIntoString, "utf-8");
outputStream = new FileOutputStream(new File("D:/test2.html"));
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
outputStream.close();
}
}
// 模擬的模板數據,實際開發從數據庫中查並保存到Map集合
private Map getMap() {
Map map = new HashMap();
map.put("name", "smilevers");
return map;
}
}