這一篇續上篇, 讓我們繼續來學習一下,Freemarker,話不多說,煌sir帶你上乾貨~~
一. 其他指令
1.運算符
- 算數運算符:FreeMarker表達式中完全支持算術運算
算數運算符 |
+ |
- |
* |
/ |
% |
- 邏輯運算符
邏輯運算符 |
描述 |
&& |
邏輯與 |
|| |
邏輯或 |
! |
邏輯非 |
- 比較運算符
比較運算符 |
描述 |
== |
判斷兩個值是否相等 |
!= |
判斷兩個值是否不等 |
>或者gt |
判斷左邊值是否大於右邊值 |
>=或者gte |
判斷左邊值是否大於等於右邊值 |
<或者lt |
判斷左邊值是否小於右邊值 |
<=或者lte |
判斷左邊值是否小於等於右邊值 |
- 注意:>、>=、<、<= 可能出現與預期結果不一致的情況,建議使用等效的字母
實例
<#if num > 60 >
num大於60
</#if>
- 如果num爲100,num表示條件成立,輸出結果:
- 60 > num 大於 60
- 建議編寫方式
<#if num gt 60 >
num大於60
</#if>
2. 空值處理
- 判斷某變量是否存在使用 “??”
變量??
,如果不爲空返回true,如果爲空返回false
Controller:
@GetMapping("/if") public String _if(Model model) { //設置數據 model.addAttribute("token", 1234); model.addAttribute("token2", "5678"); return "if"; }
if.ftl:
<#--空值處理--> <#--不爲空處理--> <#if token??> token不爲空 <br/> </#if> <#--爲空處理--> <#if !token3??> token3爲空 <br/> </#if> <#--默認值--> ${token3!'3的默認值'}<br/>
測試:
3.內建函數
內建函數語法格式: 變量+?+函數名稱
//和到某個集合的大小
${集合名?size}
//顯示年月日:
${日期?date}
//顯示時分秒:
${日期?time}
//顯示日期+時間:
${日期?datetime} <br>
//自定義格式化:
${日期?string("yyyy年MM月")}
//不想顯示爲每三位分隔的數字
${數字變量?c}
//將json字符串轉成對象
text?eval
Controller:
@GetMapping("/method") public String method(Model model) { //集合 ArrayList<String> list = new ArrayList<>(); list.add("abc"); list.add("1234"); model.addAttribute("list", list); //時間 model.addAttribute("birthday", new Date()); // 整形 model.addAttribute("num", 12345678); //JSON數據 model.addAttribute("userJSON", JSONObject.toJSONString(new User("aaa", "bbb", 33))); //設置頁面 return "method"; }
method.ftl:
顯示集合大小: ${list?size} <br/> 顯示年月日:${birthday?date} <br/> 顯示時分秒: ${birthday?time} <br/> 顯示日期+時間: ${birthday?datetime} <br/> 自定義格式化: ${birthday?string("yyyy年MM月")}<br/> ${birthday?string("yyyy/MM/dd HH:mm:ss:SSS")}<br/> 整形數據:<br/> 默認顯示:${num} <br/> 格式化:${num?c} <br/> JSON數據:<br/> 字符串:${userJSON} <br/> <#assign user=userJSON?eval /> 對象輸出:${user.username} <br/>
測試:
二.靜態化
1.模板文件靜態化
定義模板文件,使用freemarker靜態化程序生成html文件
-
步驟1:修改pom文件,添加IO依賴
- 我上篇最開始的依賴已提供(可無需再添加)
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency>
步驟二:創建 src/test/resources文件夾 ,並設置其爲資源文件夾
設置方式一:
設置方式二:
步驟三:創建測試模板文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> ${name} </body> </html>
步驟四:編寫測試類
@Test
public void tset1() throws Exception {
//根據模板生成html
// 1.核心配置類
Configuration configuration = new Configuration(Configuration.getVersion());
// 2.設置模板文件夾
// 2.1.獲得類路徑 /test_freemarker/target/test-classes/
String classpath = this.getClass().getResource("/").getPath();
System.out.println(classpath);
// 2.2.靜態目錄
// File templateDir = new File(classpath); //在resource根目錄下創,不用額外路徑
File templateDir = new File(classpath,"/templates/");
// 2.3.給配置類設置操作目錄
configuration.setDirectoryForTemplateLoading(templateDir);
// 3.設置編碼
configuration.setDefaultEncoding("UTF-8");
// 4.獲得具體模板 test.ftl
Template template = configuration.getTemplate("test.ftl");
//5.準備數據
HashMap<String, String> map = new HashMap<>();
map.put("name","ce999");
// 6.靜態haul生產,字符串內容
String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(s);
// 7.將 "字符串"寫入文件彙總
File file = new File(templateDir,"test.html");
FileUtils.writeStringToFile(file,s);
}
步驟五:測試結果
2.模板字符串靜態化
定義模板字符串,使用freemarker靜態化程序生成html文件
@Test
public void testString() throws Exception {
// 1.定義字符串模板-後期數據從數據庫查詢
String stringTemplate="<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
"${name}\n" +
"</body>\n" +
"</html>";
// 2.聲明模板加載器
StringTemplateLoader templateLoader = new StringTemplateLoader();
templateLoader.putTemplate("myTemplate",stringTemplate);
// 3.核心配置類,需要使用模板加載器,最終將字符串加載成模板
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setTemplateLoader(templateLoader);
// 4.獲得指定命名模板
Template myTemplate = configuration.getTemplate("myTemplate", "UTF-8");
// 5.準備數據
HashMap<String, String> map = new HashMap<>();
map.put("name","字符串填充666");
// 6.靜態化
String str = FreeMarkerTemplateUtils.processTemplateIntoString(myTemplate, map);
// 7.IO 寫入
String path = this.getClass().getResource("/templates").getPath();
File file = new File(path, "test1.html");
FileUtils.writeStringToFile(file,str);
}
同理:測試結果
看完恭喜你,又知道了一點點!!!
你知道的越多,不知道的越多!
~感謝志同道合的你閱讀, 你的支持是我學習的最大動力 ! 加油 ,陌生人一起努力,共勉!!