package com.test.demo;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TestFreemarker {
/**
* @param args
*/
public static void main(String[] args) {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
String templateContent="欢迎:${name}";
stringLoader.putTemplate("myTemplate",templateContent);
cfg.setTemplateLoader(stringLoader);
try {
Template template = cfg.getTemplate("myTemplate","utf-8");
Map root = new HashMap();
root.put("name", "javaboy2012");
StringWriter writer = new StringWriter();
try {
template.process(root, writer);
System.out.println(writer.toString());
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* getControlOptions: 渲染字符串dataOptions<br/>
* @param options
* @param m
* @return
* String
* @exception
* @since 1.0.0
*/
private static String getControlOptions(String options, Map<String, Object> m) {
if(StringUtils.isNoneBlank(options)){
Configuration config = new Configuration(Configuration.VERSION_2_3_23);
StringTemplateLoader stringTemplate = new StringTemplateLoader();
String templateName = "formOptions";
stringTemplate.putTemplate(templateName,options);
config.setTemplateLoader(stringTemplate);
Writer out = new StringWriter(256);
try {
Template template = config.getTemplate("formOptions","utf-8");
template.process(m, out);
} catch (TemplateException | IOException e) {
e.printStackTrace();
}
return out.toString();
}
return options;
}