热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jFinal集成Velocity的toolbox,仓促完成的,有改进的想法欢迎留言:)

2019独角兽企业重金招聘Python工程师标准packagecom.jfinal.render;importjava.io.IOException;importjava.

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.jfinal.render;import java.io.IOException;
import java.io.Writer;
import java.util.Properties;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.io.VelocityWriter;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.tools.view.ToolboxManager;
import org.apache.velocity.tools.view.context.ChainedContext;
import org.apache.velocity.tools.view.servlet.ServletToolboxManager;
import org.apache.velocity.util.SimplePool;@SuppressWarnings("deprecation")
public class VelocityWithToolboxRender extends Render
{private static final String TOOL_BOX_CONF_FILE ="/WEB-INF/toolbox.xml";private static final String encoding = getEncoding();private static final String contentType = "text/html;charset=" + encoding;protected ToolboxManager toolboxManager = null;private static SimplePool writerPool = new SimplePool(40);public VelocityWithToolboxRender(String view){this.view = view;initToolbox(RenderFactory.getServletContext());}static {String webPath = RenderFactory.getServletContext().getRealPath("/"); Properties properties = new Properties();properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, webPath);properties.setProperty(RuntimeConstants.ENCODING_DEFAULT, encoding); properties.setProperty(RuntimeConstants.INPUT_ENCODING, encoding); properties.setProperty(RuntimeConstants.OUTPUT_ENCODING, encoding); Velocity.init(properties); }protected void initToolbox(ServletContext servletContext){toolboxManager = ServletToolboxManager.getInstance(servletContext, TOOL_BOX_CONF_FILE);}@SuppressWarnings("unchecked")protected Context createContext(HttpServletRequest req, HttpServletResponse resp){ChainedContext ctx = new ChainedContext(new VelocityEngine(), req, resp, RenderFactory.getServletContext());if (toolboxManager != null){ctx.setToolbox(toolboxManager.getToolbox(ctx));}return ctx;}@Overridepublic void render(){Context context = createContext(request, response);VelocityWriter vw = null;Template template = Velocity.getTemplate(view);response.setContentType(contentType);try{Writer writer = response.getWriter();vw = (VelocityWriter) writerPool.get();if (vw == null){vw = new VelocityWriter(writer, 4 * 1024, true);}else{vw.recycle(writer);}template.merge(context, vw);}catch (IOException e){}finally{if (vw != null){try{vw.flush();vw.recycle(null);writerPool.put(vw);}catch (Exception e){Velocity.debug("VelocityViewServlet: " + "Trouble releasing VelocityWriter: " + e.getMessage());}}}}}


用法如下:写一个BaseController

@Overridepublic void render(String view){super.render(new VelocityWithToolboxRender(view));}

因为我的项目中展现层用的是Velocity,所以可以直接覆盖super class的render方法。

  


转:https://my.oschina.net/yangxiaobin/blog/62059



推荐阅读
author-avatar
real存在尹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有