packagecom.zhuqing.test;
importjava.io.IOException;
importjava.io.StringWriter;
importjava.util.Map;
importfreemarker.template.Configuration;
importfreemarker.template.TemplateException;
publicclassFreemarkerUtil{
privatefinalstaticConfiguration_CFG=newConfiguration();
publicstaticStringprint(Stringname,Maproot)throwsIOException,TemplateException{
///ftl为包名
_CFG.setClassForTemplateLoading(FreemarkerUtil.class,"/ftl");
StringWritersw=newStringWriter();
_CFG.getTemplate(name).process(root,sw);
returnsw.toString();
}
}
/////////////////////////////////////////////////////////////老大FreemarkerUtil
packagecom.xwtec.base.util;
importjava.io.StringWriter;
importjava.util.HashMap;
importjava.util.Map;
importorg.apache.log4j.Logger;
importfreemarker.cache.ClassTemplateLoader;
importfreemarker.template.Configuration;
importfreemarker.template.DefaultObjectWrapper;
importfreemarker.template.Template;
importfreemarker.template.TemplateExceptionHandler;
importfreemarker.template.Version;
publicclassFreeMarkerTemplateUtil{
privatestaticLoggerlogger=Logger.getLogger(FreeMarkerTemplateUtil.class);
privatestaticfinalConfiguration_CONFIG=newConfiguration();
static{
_CONFIG.setTemplateLoader(newClassTemplateLoader(FreeMarkerTemplateUtil.class,"/resource/template"));
_CONFIG.setObjectWrapper(newDefaultObjectWrapper());
_CONFIG.setDefaultEncoding("UTF-8");
_CONFIG.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
_CONFIG.setIncompatibleImprovements(newVersion(2,3,20));
}
publicstaticStringprocess(Stringtemplate,MapdataMap){
try{
Templatetemp=_CONFIG.getTemplate(template);
StringWritersw=newStringWriter();
temp.process(dataMap,sw);
returnsw.toString();
}
catch(Exceptione){
logger.error(e);
returnnull;
}
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Stringtmp="${ori}${home}${actcode}1${tranid}${time}973026[产品][2300220001]用户已经办理,不能再次办理!201306241";
Mapparam=newHashMap();
param.put("ori","wxcs");
param.put("home","crm");
param.put("actcode","TVsai230");
param.put("time","201308113972137");
param.put("tranid","m239009188740984");
System.out.println(FreeMarkerTemplateUtil.process("test.ftl",param));
}
}
|
|