您的位置:首页 > 编程语言 > Java开发

Java 模板引擎FreeMarker实战

2016-07-18 15:18 363 查看
FreeMarkerTest.java

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.junit.Test;

import java.io.File;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* Created by Lovell on 7/14/16.
*/
public class FreeMakerTest {
/**
* 基本加载流程
* @throws Exception
*/
@Test
public void test1() throws Exception {
// 创建freemarker配置实例
Configuration conf = new Configuration(Configuration.VERSION_2_3_22);
// 创建数据模型
conf.setDirectoryForTemplateLoading(new File("src/test/templates"));
conf.setDefaultEncoding("UTF-8");

Map<String, String> m = Maps.newHashMap();
m.put("name", "Lovell");

conf.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
// 加载模板文件
Template t = conf.getTemplate("index.ftl");
// 显示生成后的数据
OutputStreamWriter w = new OutputStreamWriter(System.out);
t.process(m, w);
w.close();
}

/**
* if的使用
* @throws Exception
*/
@Test
public void test2() throws Exception {
// 创建freemarker配置实例
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
conf.setDirectoryForTemplateLoading(new File("src/test/templates"));
// 创建数据模型
Map<String, Object> m = Maps.newHashMap();
m.put("score", new Random().nextInt(100));

UserInfo u1 = new UserInfo();
u1.setName("Lovell");
u1.setPassword("123456");
UserInfo u2 = new UserInfo();
u2.setName("柯南");
u2.setPassword("654321");
List<UserInfo> all = Lists.newArrayList();
all.add(u2);
all.add(u1);
m.put("userList", all);
m.put("time", new Date());
m.put("str", "北京,上海,杭州");
// 加载模板文件f
Template t = conf.getTemplate("useif.ftl");
// 显示生成后的数据
OutputStreamWriter w = new OutputStreamWriter(System.out);
t.process(m, w);
w.close();
}

/**
* 宏指令的使用
* @throws Exception
*/
@Test
public void test3() throws Exception {
//创建freemarker配置实例
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
conf.setDirectoryForTemplateLoading(new File("src/test/templates"));
//创建数据模型
Map<String, Object> m = Maps.newHashMap();
m.put("type","other");
m.put("num1",2);
m.put("num2",3);
//加载模板文件
Template t = conf.getTemplate("macro.ftl");
//显示生成后的数据
OutputStreamWriter w = new OutputStreamWriter(System.out);
t.process(m,w);
w.close();
}
}

 UserInfo.java

/**
* Created by Lovell on 7/14/16.
*/
public class UserInfo {

private String name;
private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}


src/test
src/test/java

src/test/resources
src/test/templates

src/test/templates/index.ftl

src/test/templates/macro.ftl

src/test/templates/useif.ftl

index.ftl

hello,${name}

useif.ftl

--------------if语句的使用---------------
<#if score gte 60>
及格
<#elseif score gte 80&&score lte 90>
良好
<#else>
高材生
</#if>
--------------空值判断、默认值------------
${name!"未定义"}
--------------判断值是否存在--------------
<#if name??>
name存在
<#else>
name不存在
</#if>
-------------使用list遍历数据-------------
<#list userList as user>
<#if  user_has_next>
最后一组:
${user.name}:{user.password}
<#else>${user.name}:${user.password}
</#if>
</#list>
-------------其他内建函数-----------------
(1)日期格式化
${time?string("yyyy-MM-dd")}
(2)截取字符串
${str?substring(0,2)}
(3)indexof的使用
${str?last_index_of(",")}
(4)split的使用
<#list "12,13,14,15"?split(",") as item>
${item}
</#list>

macro.ftl

----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为形参----------------------
<#macro m1 num1 num2>
<#assign result=num1+num2>
<h3>${result}</h3>
</#macro>
<@m1 5 6/>
----------------------宏指令(嵌入式)-----------------
<#macro m2>
<h3><#nested></h3>
</#macro>
<@m2>hello world</@m2>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: