FreeMarker模板处理实例

http://hayash.blog.163.com/blog/static/1110517201061505435508/

//处理类
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

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

public class FreeMarkerTemplateUtils {
    private final static String Template_Dir = "template";
    private final static Configuration config = new Configuration();

    static{
        URL url = FreeMarkerTemplateUtils.class.getClassLoader().getResource(Template_Dir);
        File dir  = new File(url.getFile());
        try {
            config.setDirectoryForTemplateLoading(dir);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        config.setObjectWrapper(new DefaultObjectWrapper());
        config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

    }
    
    public static Template getTemplate(String templateName){
        try {
            return config.getTemplate(templateName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static String process(Template template,Object model){
        try {
            return org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static String process(String templateName,Object model){
        return process(getTemplate(templateName),model);
    }
    

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "live");
        map.put("sign", "adbc1234");
        map.put("userID", "lamfire");
        map.put("departureDateTime", "1010-8-8");
        map.put("originLocation", "CAN");
        map.put("destinationLocation", "PEK");
        map.put("airlineCode", "AC");
        
        String content = FreeMarkerTemplateUtils.process("FlightQueryRequest.ftl", map);
        System.out.println(content);
    }
}


//模板template/FlightQueryRequest.ftl
<?xml version="1.0" encoding="UTF-8"?>
<RX_AirAvailRQ>
    <POS>
        <Source>
            <RequestorID ID="${id}" Sign="${sign}" UserID="${userID}"/>
        </Source>
    </POS>
    <OriginDestinationInformation>
        <DepartureDateTime>${departureDateTime}</DepartureDateTime>
        <OriginLocation LocationCode="${originLocation}"/>
        <DestinationLocation LocationCode="${destinationLocation}"/>
    </OriginDestinationInformation>
    <SpecificFlightInfo>
        <Airline Code="${airlineCode}"/>
    </SpecificFlightInfo>
</RX_AirAvailRQ>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值