FreeMarker在JAVA中应用入门

在项目中通常有生成XML文件发送到另一个系统的需求,简单的办法可以是用一个XML模板,通过Freemarker替换其中的'Mark'(${}),生成最终的XML文件.

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

1,创建一个XML模板:



    
        
            ${p.fname}
            ${p.gname}
        
        ${p.email}
             
        

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

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 parameters = new HashMap();

            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("[email protected]");
        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>

    <#escape x as x?xml>
    
        
            ${p.fname}
            ${p.gname}
        
        ${p.email}
        
    
    

val.setManager("m&an
***循环处理 <#list>

    <#list people as p>
    
        
            ${p.fname}
            ${p.gname}
        
        ${p.email}
        
    
    
        List pList = new ArrayList();
        ValueObject val = null;
        
        val = new ValueObject();        
        val.setFname("Yao");
        val.setGname("Yorker");
        val.setEmail("[email protected]");
        val.setManager("m&an

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

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

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

    public void generateRequest(List pList) {
        String reqFileName = "D:\\temp\\test.xml";
        try {
            Map parameters = new HashMap();
            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 pList = new ArrayList();
        ValueObject val = null;
        
        for(int i=0;i<400000;i++){
            val = new ValueObject();        
            val.setFname("Yao"+i);
            val.setGname("Yorker");
            val.setEmail("[email protected]");
            val.setManager("m&an

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

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

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

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

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

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

    <#escape x as x?xml>
    <#list people as p>
    
        
            ${p.fname}
            ${p.gname}
        
        ${p.email}
        
        <#if p.level == "L1">
        xxx
        
    
    
    

JAVA代码:

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("");
            writer.write("");
            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("");            
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();// TODO
        } 
    }
    
    public void generateRequest(List pList) {
        String reqFileName = "D:\\temp\\test.xml";
        try {
            Map parameters = new HashMap();
            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 pList = new ArrayList();
        ValueObject val = null;
        int i=0;
        for(;i<500000;i++){
            val = new ValueObject();        
            val.setFname("Yao"+i);
            val.setGname("Yorker");
            val.setEmail("[email protected]");
            val.setManager("m&an

注意在输出循环部分和结尾部分是通过追加到文件的方式:OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName,true), "UTF-8");
最终生成的XML文档有90M,没有报异常.
这里有FreeMarker的中文manual :http://download.csdn.net/user/kkdelta

你可能感兴趣的:(FreeMarker在JAVA中应用入门)