| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:FreeMarker在JAVA中应用入门
取消只看楼主 加入收藏
shitainong
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2012-7-6
结帖率:0
收藏
 问题点数:0 回复次数:0 
FreeMarker在JAVA中应用入门
在项目中通常有生成XML文件发送到另一个系统的需求,简单的办法可以是用一个XML模板,通过Freemarker替换其中的'Mark'(${}),生成最终的XML文件.

下面记录了一下简单的示例步骤:

1,创建一个XML模板:www.

[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:h="http://www.
<person id="000001" age="20">
<name>
<family>${p.fname}</family>
<given>${p.gname}</given>
</name>
<email>${p.email}</email>
<link manager="${p.manager}" />
</person>
</people>
 

2,在JAVA中将mark用值替换掉:下面是JAVA代码

[java] view plaincopyprint?
package com.test.xml.freemarker;

public class ValueObject {
private String fname;
private String gname;
private String email;
private String manager;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
}
public class Test {
Configuration freeMarkerCfg = new Configuration();
Template template = null;
public Test() {
freeMarkerCfg.setClassForTemplateLoading(getClass(),"");
freeMarkerCfg.setObjectWrapper(new DefaultObjectWrapper());
try {
template = freeMarkerCfg.getTemplate("test.xml");
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}

public void generateRequest(ValueObject obj) {

String reqFileName = "D:\\temp\\test.xml";
try {
Map<String, Object> parameters = new HashMap<String, Object>();

parameters.put("p", obj);

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName), "UTF-8");
template.process(parameters, writer);
writer.flush();
} catch (IOException e) {
e.printStackTrace();// TODO
} catch (TemplateException e) {
e.printStackTrace();// TODO
}

}
public static void main(String[] args) {
ValueObject val = new ValueObject();

val.setFname("Yao");
val.setGname("Yorker");
val.setEmail("test@);
val.setManager("manager");

new Test().generateRequest(val);
}
}
 

XML模板和class在package "com.test.xml.freemarker".

***在模板中通过${val}指定的值,如果在处理的时候val值为null,会出现下面的异常:

freemarker.core.InvalidReferenceException: Expression valis undefined on line 46, column 63 in test.ftl

FreeMarker这么做的本意是防止在传值的地方(Data Model)写入的参数,如想传入 parameters.put("abc", obj);,但是写成了parameters.put("abcd", obj);

但是有的时候,里面有的值不是必须有值的,可以通过${val!""}来绕过这个异常.${val!""}的意思是如果val为null,取值"".

***Freemarker对XML文件中特殊字符的处理:通过<#escape>

[html] view plaincopyprint?
<#escape x as x?xml>
<person id="000001" age="20">
<name>
<family>${p.fname}</family>
<given>${p.gname}</given>
</name>
<email>${p.email}</email>
<link manager="${p.manager}" />
</person>
</#escape>
 

val.setManager("m&an<ager"); 传入的值就按照XML规范保存到XML文件 link manager="m&amp;an&lt;ager" />


***循环处理 <#list>

[html] view plaincopyprint?
<#list people as p>
<person id="000001" age="20">
<name>
<family>${p.fname}</family>
<given>${p.gname}</given>
</name>
<email>${p.email}</email>
<link manager="${p.manager}" />
</person>
</#list>
List<ValueObject> pList = new ArrayList<ValueObject>();
ValueObject val = null;

val = new ValueObject();
val.setFname("Yao");
val.setGname("Yorker");
val.setEmail("test@);
val.setManager("m&an<ager");
pList.add(val);

val = new ValueObject();
val.setFname("J");
val.setGname("Jeremy");
val.setEmail("test1@);
val.setManager("m&an<ager");
pList.add(val);
parameters.put("people", pList);
 

***分支处理<#if>,根据值对模板做不同的输出.

<#if p.level == "L1">
<l1tag>xxx</l1tag>
</#if>

***对生成超大文件的测试:

[java] view plaincopyprint?
public void generateRequest(List<ValueObject> pList) {
String reqFileName = "D:\\temp\\test.xml";
try {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("people", pList);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName), "UTF-8");
template.process(parameters, writer);
writer.flush();
} catch (IOException e) {
e.printStackTrace();// TODO
} catch (TemplateException e) {
e.printStackTrace();// TODO
}
}
public static void testLargeVolumn(){
List<ValueObject> pList = new ArrayList<ValueObject>();
ValueObject val = null;

for(int i=0;i<400000;i++){
val = new ValueObject();
val.setFname("Yao"+i);
val.setGname("Yorker");
val.setEmail("test@);
val.setManager("m&an<ager");
val.setLevel("L"+i);
pList.add(val);
}
new Test().generateRequest(pList);
}
 

一次性输出,本例在达到循环400000次的时候异常:java.lang.OutOfMemoryError: Java heap space

解决办法:分多次输出到最终的输出文件中.

1,将模板需要循环的部分单独做成一个模板.

先一次输出循环模板前面的部分,分多次输出循环部分,一次输出循环部分之后的部分.

示例如下,(这里由于循环之前部分和之后部分不涉及模板替换,直接用JAVA代码输出)

模板需要循环输出的部分:

[html] view plaincopyprint?
<#escape x as x?xml>
<#list people as p>
<person id="000001" age="20">
<name>
<family>${p.fname}</family>
<given>${p.gname}</given>
</name>
<email>${p.email}</email>
<link manager="${p.manager}" />
<#if p.level == "L1">
<l1tag>xxx</l1tag>
</#if>
</person>
</#list>
</#escape>
 

JAVA代码:

[java] view plaincopyprint?
public class LargeVolumnTest {
Configuration freeMarkerCfg = new Configuration();
Template template = null;
public LargeVolumnTest() {
freeMarkerCfg.setClassForTemplateLoading(getClass(),"");
freeMarkerCfg.setObjectWrapper(new DefaultObjectWrapper());
try {
template = freeMarkerCfg.getTemplate("testL.xml");
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}

public void generateRequestHeader() {
String reqFileName = "D:\\temp\\test.xml";
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName), "UTF-8");
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.write("<people xmlns:h=\"http://www.\">");
writer.flush();
} catch (IOException e) {
e.printStackTrace();// TODO
}
}
public void generateRequestTail() {
String reqFileName = "D:\\temp\\test.xml";
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName,true), "UTF-8");
writer.write("</people>");
writer.flush();
} catch (IOException e) {
e.printStackTrace();// TODO
}
}

public void generateRequest(List<ValueObject> pList) {
String reqFileName = "D:\\temp\\test.xml";
try {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("people", pList);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName,true), "UTF-8");
template.process(parameters, writer);
writer.flush();
} catch (IOException e) {
e.printStackTrace();// TODO
} catch (TemplateException e) {
e.printStackTrace();// TODO
}
}

public void testLargeVolumn(){
generateRequestHeader();
List<ValueObject> pList = new ArrayList<ValueObject>();
ValueObject val = null;
int i=0;
for(;i<500000;i++){
val = new ValueObject();
val.setFname("Yao"+i);
val.setGname("Yorker");
val.setEmail("test@);
val.setManager("m&an<ager");
val.setLevel("L"+i);
pList.add(val);
if(i%10000==0){
generateRequest(pList);
pList.clear();
}
}
if(i%10000!=0){
generateRequest(pList);
}
generateRequestTail();
}
public static void main(String[] args) {
new LargeVolumnTest().testLargeVolumn();
}
}
 

注意在输出循环部分和结尾部分是通过追加到文件的方式:OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName,true), "UTF-8");

最终生成的XML文档有90M,没有报异常.www.

搜索更多相关主题的帖子: 000001 version manager person 
2012-07-30 16:05
快速回复:FreeMarker在JAVA中应用入门
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019051 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved