ftl文件。
jsp:
<% String topMenu = FreeMakerUtil.transformData("topMenu.ftl", rootMap); %>
<div class="topmenu">
<%=topMenu%>
</div>
private static Logger log = LoggerFactory.getLogger(FreeMakerUtil.class);
private static final String templates = "templates";
private static Configuration cfg = null;
public static final int DEFAULT_WRAPPER = 1;
public static final int BEANS_WRAPPER = 2;
public static final int SIMPLE_WRAPPER = 3;
public static String transformData(String templateName,Object rootMap,int wapperType){
StringWriter writer = new StringWriter();
try {
switch (wapperType) {
case BEANS_WRAPPER:
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
break;
case SIMPLE_WRAPPER:
cfg.setObjectWrapper(ObjectWrapper.SIMPLE_WRAPPER);
break;
default:
cfg.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
break;
}
Template template = cfg.getTemplate(templateName);
template.process(rootMap, writer);
} catch (Exception e) {
log.error("transformData",e);
}
return writer.toString();
}
public static String transformData(String templateName,Object rootMap){
return transformData(templateName,rootMap,DEFAULT_WRAPPER);
}
public static void initConfiguration(){
URL url = FreeMakerUtil.class.getClassLoader().getResource(templates);
initConfiguration(url.getPath());
}
public static void initConfiguration(String templatePath){
try {
if(cfg==null) cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setDefaultEncoding("UTF-8");
cfg.setOutputEncoding("UTF-8");
} catch (Exception e) {
log.error("initConfiguration",e);
}
}
}
1584

被折叠的 条评论
为什么被折叠?



