標籤:
其實產生靜態網頁的方式有好多種,我昨天看了一下,Freemarker是其中一種,但是Freemarker現在我們都用得比較少了,現在用得ActiveMQ用來發送資訊到靜態頁面,不過想了一下這個小東西,還是想給大家分享一下,我的小小心得。
若項目為Maven項目,那麼可以如下
在Pom.xml檔案裡面添加
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.16</version> </dependency>
CreateFreemarkerStatic.java
package com.llmj.DemoTest.Test;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;public class CreateFreeMarkStatic { /** * * @Description * @author xebest-pc * @param name * @return */ public Template getTemplate(String name) { try { // 通過Freemaker的Configuration讀取相應的ftl Configuration cfg = new Configuration(); // 設定去哪裡讀取相應的ftl模板檔案 cfg.setClassForTemplateLoading(this.getClass(), "/ftl"); // 在模板檔案目錄中找到名稱為name的檔案 Template temp = cfg.getTemplate(name); return temp; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 控制台輸出 * @Description * @author xebest-pc * @param name * @param root */ public void print(String name, Map<String, Object> root){ try { // 通過Template可以將模板檔案輸出到相應的流 Template temp = this.getTemplate(name); temp.process(root, new PrintWriter(System.out)); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 輸出HTML檔案 * @Description * @author xebest-pc * @param name * @param root * @param outFile */ public void fprint(String name, Map<String, Object> root, String outFile) { FileWriter out = null; try { // 通過一個檔案輸出資料流,就可以寫到相應的檔案中,此處用的是絕對路徑 out = new FileWriter(new File("E:/workspace/freemarkprj/page/" + outFile)); Template temp = this.getTemplate(name); temp.process(root, out); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { Map<String,Object> root=new HashMap<String,Object>(); root.put("username", "zhangsan");//在ftl中要賦值的變數 CreateFreeMarkStatic util= new CreateFreeMarkStatic(); util.fprint("01.ftl", root, "01.html"); } }
建立對應的實體類User.java
package com.llmj.DemoTest.entity;import java.io.Serializable;@SuppressWarnings("serial")public class User implements Serializable { private int id; private String name; private int age; private Group group; public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public User() { } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Group.java
package com.llmj.DemoTest.entity;public class Group { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
2 、在src目錄下建個ftl包,用於存放ftl模板檔案,this.getClass() 就是根據當前類的路徑擷取模板檔案位置
01.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>測試</title></head><body><h1>你好${username}</h1></body></html>
02.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>你好: ${username}</h1></body></html>
03.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>${user.id}-----${user.name}-----${user.age}</h1><#if user.age lt 12> ${user.name}還是一個小孩<#elseif user.age lt 18> ${user.name}快成年<#else> ${user.name}已經成年</#if></body></html>
04.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><#list users as user>${user.id}---------${user.name}-------${user.age}<br/></#list></body></html>
05.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><hr/><#list users as user>${user.id}---------${user.name}-------${user.age}<br/></#list></body></html>
06.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>${user.id}-------${user.name}------${user.group!} <#-- !後為空白就不輸出 --><#--${user.group.name!}--><#-- 按照以上的方式加! freemarker僅僅只會判斷group.name是不是空值 -->${(user.group.name)!"1234"} ${(a.b)!"沒有a.b元素"}<#--!:指定缺失變數的預設值 ??:判斷某個變數是否存在,返回boolean值 --><#if (a.b)??> <#--if後不用加$--> 不為空白<#else> 為空白</#if></body></html>
然後最後附上測試類別FreemarkTest.java
import java.util.HashMap;import java.util.List;import java.util.Map;import org.junit.Test;import com.llmj.DemoTest.entity.Group;import com.llmj.DemoTest.entity.User;public class FreemarkerTest { @Test public void test(){ CreateFreeMarkStatic util = new CreateFreeMarkStatic(); Map<String, Object> map = new HashMap<String, Object>(); Group group = new Group(); group.setName("IT"); User user = new User(); user.setId(001); user.setName("張三"); user.setAge(12); user.setGroup(group); List<User> users = new ArrayList<User>(); users.add(user); users.add(user); users.add(user); map.put("user", user); //普通EL賦值 //util.fprint("01.ftl", map, "01.html" ); //判斷 //util.fprint("03.ftl", map, "03.html"); //遍曆 //util.print("05.ftl", map); //子項目判斷 util.print("06.ftl", map); }}
這樣就可以測試了
Freemarker入門小案例(產生靜態網頁的其中一種方式)