http://blog.csdn.net/marksinoberg/article/details/52006311
***************************************************
今天分享一下一个模板语言的使用,它就是Freemarker,有点类似与前些日子做Python的Django中的模板语言,其实原理上都是相似的。所以这里就不对那些基础性的语法类的直至进行讲解了,就拿几个实用的小例子来分析分析。
我们需要导入一个jar包,名为freemarker.jar。随便到网上下载一个就行,而且对其他诸如servlet等没有依赖,所以我们可以很轻松的进行移植操作。
我使用的第一个简单的模板01.ftl如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试</title> </head> <body> <h1>你好${username}</h1> </body> </html> 12345678910 12345678910测试代码如下:
/** * 仅仅针对有一个数据的测试 * * @throws Exception */ @Test public void testftl1() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("01.ftl"); Map<String, Object> map = new HashMap<String, Object>(); map.put("username", "XIAO MARK"); // 默认输出到了控制台上 template.process(map, new OutputStreamWriter(System.out)); } 1234567891011121314 1234567891011121314使用到的模板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> 12345678910111213141516 12345678910111213141516我们可以从模板中看到user.id,那就说明我们使用到了对象,所以UserBean 详情如下。
package main; import java.io.Serializable; public class User implements Serializable { private int id; private String name; private int age; private Group group; 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; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", group=" + group + "]"; } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950内有组合类Group的使用,
package main; import java.io.Serializable; public class Group implements Serializable{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Group [name=" + name + "]"; } } 1234567891011121314151617181920212223 1234567891011121314151617181920212223测试代码如下:
/** * 控制台输出带有对象的模板使用案例 * * @throws Exception */ @Test public void testftl3() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); User user = new User(); user.setId(1); user.setName(" 妈的智障 "); user.setAge(21); map.put("user", user); template.process(map, new OutputStreamWriter(System.out)); } 1234567891011121314151617 1234567891011121314151617 /** * 输出HTML文件形式的带有对象的测试案例 * * @throws Exception */ @Test public void testftl3outtofile() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); User user = new User(); user.setId(1); user.setName(" 妈的智障 "); user.setAge(21); map.put("user", user); util.htmlprint("03.ftl", map, "./../page/03ftloutfile.html"); } 1234567891011121314151617 1234567891011121314151617使用到的模板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> 12345678910111213 12345678910111213测试代码如下:
/** * 输出HTML文件形式的带有对象的测试案例 * * @throws Exception */ @Test public void testftl5outtofile() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); List<User> users = new ArrayList<User>(); for (int i = 1; i <= 10; i++) { User user = new User(); user.setId(i); user.setName(" 妈的智障 " + (i * i)); user.setAge((int) (Math.random() * 100)); users.add(user); } map.put("users", users); util.htmlprint("05.ftl", map, "./../page/05ftloutfile.html"); } 123456789101112131415161718192021 123456789101112131415161718192021使用到的模板文件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> 123456789101112131415161718192021222324 123456789101112131415161718192021222324测试代码如下:
@Test public void testftl6() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Map<String,Object > map = new HashMap<String,Object>(); User user = new User(); Group group = new Group(); group.setName("1234"); user.setId(28); user.setAge(27); user.setName(" 妈的智障 "); user.setGroup(group); map.put("user", user); util.print("06.ftl", map); } 1234567891011121314 1234567891011121314