博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jFinal 集成Velocity的toolbox,仓促完成的,有改进的想法欢迎留言:)
阅读量:5904 次
发布时间:2019-06-19

本文共 3477 字,大约阅读时间需要 11 分钟。

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;   }   @Override   public 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
@Override   public void render(String view)   {      super.render(new VelocityWithToolboxRender(view));   }

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

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

你可能感兴趣的文章
虚拟桌面带宽评估
查看>>
一起学shell(十一)之安全的shell脚本:起点
查看>>
Microsoft® Deployment Toolkit 2010之快速部署Windows 7
查看>>
vue中this.$router.push()传参简单说明
查看>>
html中base标签的用处
查看>>
LNMP的技术讲解
查看>>
前端基础入门一(HTML)
查看>>
再不努力,就真的没有机会了
查看>>
SVN Hooks的介绍及使用
查看>>
【Demo】HTML5 拍照上传
查看>>
路由器设置密码和标语
查看>>
CSS3属性
查看>>
Oracle 字符集的查看和修改【上】
查看>>
JQuery 基础操作
查看>>
linux 脚本之 expect命令使用
查看>>
沃通SSL证书支持ECC算法吗?
查看>>
javassist用法总结
查看>>
求dojo/domReady不能在sync loader下使用的原因
查看>>
好程序员web前端分享CSS3弹性盒
查看>>
游戏UI框架设计(二) : 最简版本设计
查看>>