guanxf

我的博客:http://blog.sina.com.cn/17learning

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  71 随笔 :: 1 文章 :: 41 评论 :: 0 Trackbacks
1、Controller:
               Map<String, Object> rootMap = new HashMap<String, Object>();
ootMap.put("list",getMusicPublisList(page.getList()));
String templateName="appweb/music/index.ftl";
  Render appRender=new AppFreeMarkerRender(rootMap,templateName);
  this.render(appRender);
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;
}
posted on 2013-05-19 14:16 管先飞 阅读(4946) 评论(2)  编辑  收藏 所属分类: Jfinal

评论

# re: Jfinal读取Freemarker后renderHtml 源码 2015-09-23 15:53 斯蒂芬
撒的发生  回复  更多评论
  

# re: Jfinal读取Freemarker后renderHtml 源码 2015-09-23 15:53 斯蒂芬
撒的发生的  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: