welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ:9167702333 邮箱:admin@xlongwei.com

VelocityUtilView使用Velocity Tools 2.0的代码


分类 Java   关键字 分享   标签 java   web   发布 hongwei  1427515457861
注意 转载须保留原文链接,译文链接,作者译者等信息。  
我要赚钱:注册财富箱,享投资12%收益!


ServletToolboxManager has been deprecated. Please use org.apache.velocity.tools.ToolboxFactory instead.
VelocityUtilView是一个View(用于渲染vm页面),也是一个Util(用于生成vm邮件内容等),再配置新版的tools.xml,就可以使用上Velocity Tools 2.0的API了。

配置velocityViewResolver,使用自定义的viewClass
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
	<property name="cache" value="true" />
	<property name="exposeSpringMacroHelpers" value="true" />
	<property name="requestContextAttribute" value="true" />
	<property name="exposeSessionAttributes" value="true" />
	<property name="allowSessionOverride" value="true" />

	<property name="prefix" value=""></property>
	<property name="suffix" value=".vm"></property>
	<property name="order" value="1"></property>
	<property name="contentType" value="text/html; charset=UTF-8"></property>
	<property name="toolboxConfigLocation" value="/WEB-INF/velocity-toolbox.xml"></property>
   <!-- <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityToolboxView" /> -->
	<property name="viewClass" value="com.itecheast.ite.front.util.VelocityUtilView" />
</bean>
配置velocity-toolbox.xml,沿用了之前的文件名,但内容格式得更新。
velocity默认加载了很多工具,参考/org/apache/velocity/tools/generic/tools.xml
<?xml version="1.0"?>
<tools>
	<toolbox scope="application">
		<tool key="date" class="org.apache.velocity.tools.generic.DateTool"/>
		<tool key="shiro" class="com.itecheast.ite.domain.shiro.ShiroTool"/>
		<tool key="props" class="com.itecheast.ite.domain.util.GlobalConfig"/>
		<tool key="tools" class="com.itecheast.ite.domain.util.VelocityTool"/>
	</toolbox>
</tools>
VelocityUtilView
public class VelocityUtilView extends VelocityToolboxView {
	private static ToolManager tm;
	private static VelocityEngine ve;
	private static Toolbox ta, ts, tr;
    //静态方法供发邮件时调用
    public static String merge(String vm, Map<String, Object> model) {
    	if(tm!=null) {
			ToolContext ctx = (ToolContext)tm.createContext();
			if(model!=null) ctx.putAll(model);
			StringWriter out = new StringWriter();
			ve.mergeTemplate(vm, "utf-8", ctx, out);
	    	return out.toString();
    	}else {
    		return null;
    	}
    }
	//生成view时添加toolbox到context
	protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ViewToolContext ctx = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());
		ctx.putAll(model);
		if (tm != null || this.getToolboxConfigLocation() != null) {
			if (tm == null) {
				tm = new ToolManager();
				tm.setVelocityEngine(getVelocityEngine());
				tm.configure(getServletContext().getRealPath(getToolboxConfigLocation()));
				
				ve = getVelocityEngine();
				ta = tm.getToolboxFactory().createToolbox(Scope.APPLICATION);
				ts = tm.getToolboxFactory().createToolbox(Scope.SESSION);
				tr = tm.getToolboxFactory().createToolbox(Scope.REQUEST);
			}
			ctx.addToolbox(ta);
			ctx.addToolbox(ts);
			ctx.addToolbox(tr);
		}
		return ctx;
	}
}