本文主要是介绍Spring MVC 4.0 + Velocity Tools 2.0遇到的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、Spring MVC 4.0及更高版本不建议使用Velocity模板引擎,为了继承Velocity并使用Velocity Tools,需要重写viewClass,如下:
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.tools.ToolboxFactory;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.servlet.view.velocity.VelocityLayoutView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;public class VelocityLayoutToolbox2View extends VelocityLayoutView {private static ToolboxFactory toolboxFactory = null;@Overrideprotected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {// Create a ChainedContext instance.ViewToolContext vtc;vtc = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());vtc.putAll(model);if (toolboxFactory == null) {ToolManager toolManager = new ToolManager();toolManager.setVelocityEngine(getVelocityEngine());toolManager.configure(getServletContext().getRealPath(getToolboxConfigLocation()));toolboxFactory = toolManager.getToolboxFactory();}if (this.getToolboxConfigLocation() != null) {if (toolboxFactory.hasTools(Scope.REQUEST)) {vtc.addToolbox(toolboxFactory.createToolbox(Scope.REQUEST));}if (toolboxFactory.hasTools(Scope.APPLICATION)) {vtc.addToolbox(toolboxFactory.createToolbox(Scope.APPLICATION));}if (toolboxFactory.hasTools(Scope.SESSION)) {vtc.addToolbox(toolboxFactory.createToolbox(Scope.SESSION));}}return vtc;}
}
重写后,在applicationContext.xml文件中配置viewClass,如下:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"><property name="suffix" value=".html" /><property name="cache" value="false" /><property name="contentType" value="text/html;charset=utf-8" /><!-- 日期函数名称 --><property name="dateToolAttribute" value="date" /><!-- 数字函数名称 --><property name="numberToolAttribute" value="number" /><property name="layoutUrl" value="layout/default.vm" /><property name="viewClass" value="com.acs.survey.util.VelocityLayoutToolbox2View" /><property name="toolboxConfigLocation" value="/WEB-INF/velocity-tools.xml" /></bean>
2、Velocity Tools 2.0的配置文件格式和低版本的不同,这是个大坑!!!具体如下:
===低版本格式
<toolbox><tool><key>SSOPermission</key><scope>application</scope><class>com.baomidou.springwind.common.SSOPermissionTool</class></tool><tool><key>date</key><scope>application</scope><class>org.apache.velocity.tools.generic.DateTool</class></tool><tool><key>math</key><scope>application</scope><class>org.apache.velocity.tools.generic.MathTool</class></tool><tool><key>number</key><scope>application</scope><class>org.apache.velocity.tools.generic.NumberTool</class></tool><tool><key>render</key><scope>application</scope><class>org.apache.velocity.tools.generic.RenderTool</class></tool><tool><key>esc</key><scope>application</scope><class>org.apache.velocity.tools.generic.EscapeTool</class></tool><tool><key>alternator</key><scope>application</scope><class>org.apache.velocity.tools.generic.AlternatorTool</class></tool><tool><key>parser</key><scope>application</scope><class>org.apache.velocity.tools.generic.ValueParser</class></tool><tool><key>sorter</key><scope>application</scope><class>org.apache.velocity.tools.generic.SortTool</class></tool>
</toolbox>
===2.0格式
<tools><toolbox scope="application"><tool class="com.acs.survey.util.PermissionTool" /><tool class="org.apache.velocity.tools.generic.DateTool" /><tool class="org.apache.velocity.tools.generic.MathTool" /><tool class="org.apache.velocity.tools.generic.NumberTool" /><tool class="org.apache.velocity.tools.generic.RenderTool" /><tool class="org.apache.velocity.tools.generic.EscapeTool" /><tool class="org.apache.velocity.tools.generic.AlternatorTool" /><tool class="org.apache.velocity.tools.generic.ValueParser" /><tool class="org.apache.velocity.tools.generic.SortTool" /></toolbox><toolbox scope="request"><tool class="org.apache.velocity.tools.view.LinkTool" /></toolbox>
</tools>
这篇关于Spring MVC 4.0 + Velocity Tools 2.0遇到的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!