java使用freemarker生成靜態html頁面


1. 模板文件static.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

描述:${description}
<br/>
集合大小:${nameList?size}
<br/>
迭代list集合:
<br/>
<#list nameList as names>
這是第${names_index+1}個人,叫做:<label style="color:red">${names}</label>
if判斷:
<br/>
<#if (names=="陳靖仇")>
他的武器是: 十五~~
<#elseif (names=="宇文拓")> <#--注意這里沒有返回而是在最后面-->
他的武器是: 軒轅劍~·
<#else>
她的絕招是:蠱毒~~
</#if>
<br/>
</#list>
迭代map集合:
<br/>
<#list weaponMap?keys as key>
key--->${key}<br/>
value----->${weaponMap[key]!("null")}
<#--
fremarker 不支持null, 可以用! 來代替為空的值。
其實也可以給一個默認值
value-----${weaponMap[key]?default("null")}
還可以 在輸出前判斷是否為null
<#if weaponMap[key]??></#if>都可以
-->

<br/>
</#list>

 

</body>
</html>

 

 

2.生成代碼:

package com.xijie.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
* @description
* @author ChangGui Pan
* @date 2018年7月19日 上午11:38:53
*/
public class FreemarkerTest {

/**
* @description
* @author ChangGui Pan
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
//創建一個合適的Configration對象
Configuration configuration = new Configuration();
// configuration.setDirectoryForTemplateLoading(new File("C:\\MyWork\\Workspace\\Xijie\\WebRoot\\html"));
configuration.setDirectoryForTemplateLoading(new File("./WebRoot/html"));
configuration.setObjectWrapper(new DefaultObjectWrapper());
configuration.setDefaultEncoding("UTF-8"); //這個一定要設置,不然在生成的頁面中 會亂碼
//獲取或創建一個模版。
Template template = configuration.getTemplate("static.html");
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("description", "我正在學習使用Freemarker生成靜態文件!");

List<String> nameList = new ArrayList<String>();
nameList.add("陳靖仇");
nameList.add("玉兒");
nameList.add("宇文拓");
paramMap.put("nameList", nameList);

Map<String, Object> weaponMap = new HashMap<String, Object>();
weaponMap.put("first", "軒轅劍");
weaponMap.put("second", "崆峒印");
weaponMap.put("third", "女媧石");
weaponMap.put("fourth", "神農鼎");
weaponMap.put("fifth", "伏羲琴");
weaponMap.put("sixth", "昆侖鏡");
weaponMap.put("seventh", null);
paramMap.put("weaponMap", weaponMap);

Writer writer = new OutputStreamWriter(new FileOutputStream("success.html"),"UTF-8");
template.process(paramMap, writer);

System.out.println("恭喜,生成成功~~");
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM