1.什麼是freemarker
FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來生成文本輸出。FreeMarker與Web容器無關,即在Web運行時,它並不知道Servlet或HTTP。它不僅可以用作表現層的實現技術,而且還可以用於生成XML,JSP或Java 等。
2.Freemarker的使用方法
public class TestFreemarker {
@Test
public void test01() throws Exception{
//第一步:把freemarker的jar包添加到工程中freemarker-2.3.23.jar
//第二步:freemarker的運行不依賴web容器,可以在java工程中運行。創建一個測試方法進行測試。
//第三步:創建一個Configration對象
Configuration configuration = new Configuration(Configuration.getVersion());
//第四步:告訴config對象模板文件存放的路徑。
configuration.setDirectoryForTemplateLoading(new File("G:\\eclipse-jee-mars-2\\workspace\\freemarkerDemo\\src\\main\\webapp\\WEB-INF\\ftl"));
//第五步:設置config的默認字符集。一般是utf-8
configuration.setDefaultEncoding("UTF-8");
//第六步:從config對象中獲得模板對象。需要制定一個模板文件的名字。
Template template = configuration.getTemplate("first.ftl");
//創建模板需要的數據集。可以是一個map對象也可以是一個pojo,把模板需要的數據都放入數據集。
Map root = new HashMap<>();
root.put("hello", "hello freemarker!");
//第八步:創建一個Writer對象,指定生成的文件保存的路徑及文件名。
Writer writer = new FileWriter(new File("G:\\eclipse-jee-mars-2\\workspace\\freemarker\\hello.html"));
//第九步:調用模板對象的process方法生成靜態文件。需要兩個參數數據集和writer對象。
template.process(root, writer);
//第十步:關閉writer對象。
writer.flush();
writer.close();
}
}
模版first.ftl內容爲${hello},運行結果如下
3.Freemarker模板的基本寫法
- 3.1 簡單數據類型數據
使用EL表達式${hello},如上所示。 - 3.2 包裝數據類型
@Test
public void Test02() throws Exception{
...
Map root = new HashMap<>();
root.put("title", "包裝數據類型");
root.put("student", new Student("1", "張三", "上海"));
...
}
模版second.ftl內容爲
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
<body>
<label>學號:</label>${student.id}<br>
<label>姓名:</label>${student.name}<br>
<label>住址:</label>${student.address}<br>
</body>
</html>
- 3.3 歷遍集合/數組
@Test
public void Test02() throws Exception{
...
Map root = new HashMap<>();
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("2", "李四2", "北京"));
studentList.add(new Student("3", "李四3", "北京"));
studentList.add(new Student("4", "李四4", "北京"));
studentList.add(new Student("5", "李四5", "北京"));
studentList.add(new Student("6", "李四6", "北京"));
root.put("studentList", studentList);
...
}
模版second.ftl內容爲
<table border="1">
<#list studentList as s>
<#if s_index%2 == 0>
<tr style="color:red">
<#else>
<tr>
</#if>
<td>${s_index}</td>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.address}</td>
</tr>
</#list>
</table>
- 3.4 獲得當前迭代的索引
獲取當前選代的索引
<#list studentList as s>
${s_index}
</#list>
- 3.5 模板中判斷條件
邏輯運算符(== != || &&)
<#if 判斷條件>
<#else>
</#if>
- 3.6 日期類型格式化
默認格式
1:date
${cur_time?date}
2:datetime
${cur_time?datetime}
3:time
${cur_time?time}
自定義格式
${cur_time?string("yyyy-MM-dd HH:mm:ss")}
@Test
public void Test02() throws Exception{
...
root.put("curDate", new Date());
...
}
模版second.ftl內容爲
當前日期:${curDate?date}<br/>
當前日期:${curDate?time}<br/>
當前日期:${curDate?datetime}<br/>
當前日期:${curDate?string("yyyy/MM/dd HH:mm:ss")}<br/>
- 3.7 處理null值
@Test
public void Test02() throws Exception{
...
root.put("cur_Date", null);
root.put("val1", null);
root.put("val2", "");
...
}
模版second.ftl內容爲
<#if cur_Date ??>
當前日期:${cur_Date?string("yyyy/MM/dd HH:mm:ss")}<br/>
<#else>
cur_Date屬性爲null<br/>
</#if>
獲取null:${val1!"我是默認值"}<br/>
獲取"":${val2!}<br/>
- 3.8 include引入頁面
將另一個頁面引入本頁面時可用以下命令完成
<#include "first.ftl">
@Test
public void Test02() throws Exception{
...
root.put("hello", "first中的hello");
...
}
模版second.ftl內容爲
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<#include "first.ftl">
<title>${title}</title>
</head>
<body>
</body>
</html>
4.spring中使用freemarker
1.使用freemarker整合spring,把Configuration交給spring容器管理,依賴的jar包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2.spring配置文件內容
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
3.注入FreeMarkerConfigurer
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
...
//生成靜態網頁
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Template template = configuration.getTemplate("demo.ftl");
...