【javascript学习指南】java中使用freemarker 读取字符串模板的例子

更新时间:2019-05-07    来源:js教程    手机版     字体:

【www.bbyears.com--js教程】

代码如下

 代码如下

package com.venustech.generate;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class GenerateRule {
public void generateMain() throws IOException, TemplateException {
 /* 在整个应用的生命周期中,这个工作你应该只做一次。 */
/* 创建和调整配置。 */
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("myTemplate", "rule " +
"<#if action??> " +
" ${action} " +
" " +
"<#if protocol??> " +
" ${protocol} " +
" " +
"<#if source_ip_address??> " +
" <#if source_wildcard??> " +
" source ${source_ip_address} ${source_wildcard} " +
" " +
" " +
"<#if destination_ip_address??> " +
" <#if destination_wildcard??> " +
" destination ${destination_ip_address} ${destination_wildcard} " +
" " +
" " +
"<#if operator??> " +
" <#if destination_port??> " +
" destination_port ${operator} ${destination_port} " +
" " +
"");
cfg.setTemplateLoader(stringLoader);
Template temp = cfg.getTemplate("myTemplate","utf-8");
/* 创建数据模型 */
Map root = new HashMap();
root.put("action"  ,"deny"  );
root.put("protocol" ,"udp"  );
root.put("source_ip_address"  ,"192.168.56.123"  );
root.put("source_wildcard"  ,"0.0.0.255"  );
root.put("destination_ip_address"  ,"192.168.56.123"  );
root.put("destination_wildcard"  ,"0.0.0.255"  );
root.put("operator"  ,"gt"  );
root.put("destination_port"  ,"128"  );
Writer out = new StringWriter(2048);
temp.process(root, out);
System.out.println(out.toString().replaceAll("[nr]", ""));
out.flush();
}

public static void main(String[] args) throws Exception {
GenerateRule html = new GenerateRule();
html.generateMain();
}
}

本文来源:http://www.bbyears.com/wangyezhizuo/49975.html

热门标签

更多>>

本类排行