2、AppFreeMarkerRender(在FreeMarkerRender上修改):
package com.microcorecn.common.utils;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
/**
* FreeMarkerRender.
*/
public class AppFreeMarkerRender extends Render {
private static final long serialVersionUID = -7649769283048920381L;
private transient static final String encoding = getEncoding();
private transient static final String contentType = "text/html; charset=" + encoding;
private String templateName;
private Map<String, Object> rootMap;
public AppFreeMarkerRender(Map<String, Object> rootMap, String templateName) {
this.templateName=templateName;
this.rootMap=rootMap;
}
@SuppressWarnings({"unchecked", "rawtypes"})
public void render() {
response.setContentType(contentType);
Enumeration<String> attrs = request.getAttributeNames();
Map root = new HashMap();
while (attrs.hasMoreElements()) {
String attrName = attrs.nextElement();
root.put(attrName, request.getAttribute(attrName));
}
root.putAll(rootMap);
PrintWriter writer = null;
try {
Template template = FreemarkerUtil.getAppConfiguration().getTemplate(this.templateName);
writer = response.getWriter(); //一边请求,一边render
template.process(root, writer); // Merge the data-model and the template
} catch (Exception e) {
throw new RenderException(e);
}
finally {
if (writer != null)
writer.close();
}
}
}
3、FreemarkerUtil :
package com.microcorecn.common.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jfinal.kit.PathKit;
import com.jfinal.render.FreeMarkerRender;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
/**
* freemarker生成html的工具类
*
* @author Administrator
*
*/
public class FreemarkerUtil {
//保存所有参数,js路径,cs路径等
private static Map<String, Object> releaseParams = new HashMap<String, Object>();
static{
//releaseParams.put("csspath", "");
}
//配置
private static Configuration config = null;
private static Configuration appConfig = null;
/**
* appConfig配置所有参数
* 重写freemarker中的 reader方法,读取该配置文件
* @return
*/
public static Configuration getAppConfiguration()
{
if(appConfig == null)
{
//从freemarker中获取所有配置
appConfig = (Configuration)FreeMarkerRender.getConfiguration().clone();
try {
//设置模板路径
/* config.setDirectoryForTemplateLoading(new File(PathKit.getWebRootPath()));
config.setObjectWrapper(new DefaultObjectWrapper());*/
appConfig.setDirectoryForTemplateLoading(new File(PathKit.getWebRootPath()+"/WEB-INF/views/"));
appConfig.setObjectWrapper(new DefaultObjectWrapper());
} catch (IOException e) {
// TODO log
}
}
return appConfig;
}