導(dǎo)入pom.xml中的依賴文件:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
1. 取變量的值:
${}
取變量的值.png
2. 導(dǎo)入文件:
<#include "header.ftl" >
導(dǎo)入文件.png
3. 日期時間:
${question.createdDate?string("yyyy-MM-dd HH:mm:ss")}
日期時間.png
效果圖.png
4. 列表展示:
<#list questions as ques>
${ques.createdDate?string("yyyy-MM-dd HH:mm:ss")}<br>
</#list>
Controller層Java代碼:
@RequestMapping("/date")
public String testDate(ModelMap map) {
Question question = new Question();
question.setCreatedDate(new Date());
map.addAttribute("question", question);
List<Question> questions = new ArrayList<>();
questions.add(new Question(new Date(), 5));
questions.add(question);
map.addAttribute("questions", questions);
return "/datetime";
}
5. 默認(rèn)值的設(shè)置:
通過設(shè)置默認(rèn)值
${name!'null'}
來避免對象為空的錯誤。
如果name為空,就以默認(rèn)值(!后的字符)顯示。
6. if else的用法
<#if (question1.liked > 0)>
<p>int數(shù)值大于0</p>
<#else>
<p>int數(shù)值不大于0</p>
</#if>
所有結(jié)果.png
若見完整代碼, 下載地址:
https://github.com/menglanyingfei/Java/blob/master/CodeCollection/JavaWebProjects/freemarker.zip
總結(jié)完畢!