net.sourceforge.jweb.util.FreeMarkerUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jweb-common Show documentation
Show all versions of jweb-common Show documentation
本项目主要弥补在使用mybatis3+springmvc+jquery easyui快速搭建web应用系统是遇到的框架不足.
主要工作包括:
1,扩展了ApplicationContextAware,通过单例注入spring容器,提供spring容器外的bean获取方法
2,扩展了apache shiro框架,统一了安全结构
3,扩展了mybatis3拦截器,在两个拦截器中自动完成分页注入,实现内存分页。
4,分页设计数据库方言
5,提供了一个easyuigrid的模型
6,提供了java泛型的jstl
7, xml工具包等一些小工具
The newest version!
package net.sourceforge.jweb.util;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.cache.NullCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import net.sourceforge.jweb.util.ExceptionBuilder;
public class FreeMarkerUtil {
private static final String base = "/freemarker/";
private final static Logger LOGGER = LoggerFactory.getLogger(FreeMarkerUtil.class);
@SuppressWarnings("deprecation")
private final static Configuration freemarkerCfg = new Configuration();
static {
try {
freemarkerCfg.setClassForTemplateLoading(FreeMarkerUtil.class, base);
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
freemarkerCfg.setCacheStorage(new NullCacheStorage());
} catch (Exception e) {
e.printStackTrace();
}
}
private FreeMarkerUtil() {
}
public static String processTemplate(String name, Map param) {
// LOGGER.debug("processinng "+name+" with "+param);
try {
Writer writer = new StringWriter();
Template template = freemarkerCfg.getTemplate(name);
template.process(param, writer);
return writer.toString();
} catch (IOException e) {
String stack = ExceptionBuilder.buildStack(e);
LOGGER.error(stack);
return stack;
} catch (TemplateException e) {
String stack = ExceptionBuilder.buildStack(e);
LOGGER.error(stack);
return stack;
}
}
public static String processTemplate(String templateName, Object... params) {
Map model = new HashMap();
for (int i = 0; i < params.length;) {
model.put(params[i++].toString(), params[i++]);
}
return FreeMarkerUtil.processTemplate(templateName, model);
}
}