FreeMarker 是一個用 Java 語言編寫的模板引擎,它基于模板來生成文本輸出。FreeMarker與 Web 容器無關,即在 Web 運行時,它并不知道 Servlet 或 HTTP。它不僅可以用作表現層的實現技術,而且還可以用于生成 XML,JSP 或 Java 等。
目前企業中:主要用 Freemarker 做靜態頁面或是頁面展示
/**
* freemark入門案例
* freemark三要素:
* 1.freemark API
* 2.數據
* 3.模板文件:ftl文件
* @throws Exception
*/
@Test
public void test1() throws Exception{
//創建freemarker核心配置對象,指定freemarker
Configuration cf = new Configuration(Configuration.getVersion());
//指定服務器模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//指定模板文件編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下面獲取模板文件對象
Template template = cf.getTemplate("hello.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMapString,Object>();
maps.put("hello", "freemarker入門案例");
//創建一個輸出對象,把數據輸出daoHtml頁面
Writer out = new FileWriter(new File("F:\\template\\out\\quickstart.html"));
//生成Html頁面
template.process(maps, out);
//關閉資源
out.close();
}
/**
* freemarker模板語法處理特殊數據格式
* 例如:$0.2,20%
* 模板語法:$0.2:${price?string.currency}
* 20%:${price?string.percent}
* @throws Exception
*/
@Test
public void test2() throws Exception{
//創建freemark核心配置對象,指定freemark版本
Configuration cf = new Configuration(Configuration.getVersion());
//指定模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//設置模板編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下面取模板文件對象
Template template = cf.getTemplate("num.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMap>();
maps.put("price", 0.2);
//創建輸出對象,把數據輸出到Html頁面
Writer out = new FileWriter(new File("F:\\template\\out\\price.html"));
//生成Html頁面
template.process(maps, out);
//關閉資源
out.close();
}
/**
* 使用模板語法處理null值
* 模板文件處理語法:
* 1.?
* 語法:${username?default("張三")}
* 2.!
* 語法:
* ${username!}
* ${username!"默認值"}
* 3.if
* 語法:
* #if username??>
* ${username}
* /#if>
* @throws Exception
*/
@Test
public void test3() throws Exception{
//創建freemark核心配置對象,指定freemarker版本
Configuration cf = new Configuration(Configuration.getVersion());
//指定模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//設置模板編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下獲取模板文件對象
Template template = cf.getTemplate("null.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMap>();
maps.put("username", null);
//創建輸出對象,把數據輸出到html頁面
Writer out = new FileWriter(new File("F:\\template\\out\\username.html"));
//生成html頁面
template.process(maps, out);
//關閉資源
out.close();
}
/**
* 使用模板語法處理pojo數據
* el表達式獲取數據:
* model.addAttribute("p",person);
* ${p.username}
* ${p.address}
* 模板語法獲取pojo數據
* model.addAttribute("p",person);
* ${p.username}
* ${p.address}
* @throws Exception
*/
@Test
public void test4() throws Exception{
//創建freemark核心配置對象,指定freemarker版本
Configuration cf = new Configuration(Configuration.getVersion());
//指定模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//設置模板編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下獲取模板文件對象
Template template = cf.getTemplate("person.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMap>();
//創建person對象
Person person = new Person();
person.setUsername("張三");
person.setAge(22);
maps.put("p", person);
//創建輸出對象,把數據輸出到html頁面
Writer out = new FileWriter(new File("F:\\template\\out\\person.html"));
//生成html頁面
template.process(maps, out);
//關閉資源
out.close();
}
/**
* 使用模板語法處理集合數據
* el表達式獲取數據:
* model.addAttribute("pList",pList);
* c:foreach item="pList" var="p">
* ${p.username}
* ${p.age}
* /c:foreach>
* 模板語法獲取list數據
* model.addAttribute("pList",pList);
* #list pList as p>
* ${p.username}
* ${p.age}
* /#list>
* 角標語法:${別名_index}
* @throws Exception
*/
@Test
public void test5() throws Exception{
//創建freemark核心配置對象,指定freemarker版本
Configuration cf = new Configuration(Configuration.getVersion());
//指定模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//設置模板編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下獲取模板文件對象
Template template = cf.getTemplate("list.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMap>();
//創建list集合
ListPerson> pList = new ArrayList>();
//創建person對象
Person person1 = new Person();
person1.setUsername("張三");
person1.setAge(22);
//創建person對象
Person person2 = new Person();
person2.setUsername("李四");
person2.setAge(24);
pList.add(person1);
pList.add(person2);
maps.put("pList", pList);
//創建輸出對象,把數據輸出到html頁面
Writer out = new FileWriter(new File("F:\\template\\out\\list.html"));
//生成html頁面
template.process(maps, out);
//關閉資源
out.close();
}
/**
* 使用模板語法處理時間類型數據
* 獲取數據日期:${today?date}
* 獲取數據時間:${today?time}
* 獲取數據日期時間:${today?datetime}
* 獲取數據日期時間格式化:${today?string('yyyy-MM-dd')}
* @throws Exception
*/
@Test
public void test6() throws Exception{
//創建freemark核心配置對象,指定freemarker版本
Configuration cf = new Configuration(Configuration.getVersion());
//指定模板文件所在路徑
cf.setDirectoryForTemplateLoading(new File("F:\\template"));
//設置模板編碼
cf.setDefaultEncoding("utf-8");
//從模板文件路徑下獲取模板文件對象
Template template = cf.getTemplate("date.ftl");
//創建map對象,封裝模板數據
MapString,Object> maps = new HashMap>();
maps.put("today", new Date());
//創建輸出對象,把數據輸出到html頁面
Writer out = new FileWriter(new File("F:\\template\\out\\date.html"));
//生成html頁面
template.process(maps, out);
//關閉資源
out.close();
}
!-- 加載springmvc -->
servlet>
servlet-name>springmvc/servlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServlet/servlet-class>
init-param>
param-name>contextConfigLocation/param-name>
param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml/param-value>
/init-param>
load-on-startup>1/load-on-startup>
/servlet>