最近的项目中用到了spring mvc 版本为 4.1.6.RELAESE
而velocity-tools也被我2B的升级成了 2.0了。
------------------------------
其中mvc 3版本以上需要自定义 viewClass以完成 velocity tools的装载,不然报错
这一步,网上已经有人share出来方法了:
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <!--<property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityView"/>--> <property name="viewClass" value="com.*.*.utils.VelocityToolView" /> <property name="prefix" value="/"/> <property name="suffix" value=".vm"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/></bean>
其中代码大致如下:
VelocityToolView VelocityToolboxView { Context (Map<StringObject> modelHttpServletRequest requestHttpServletResponse response) Exception {ViewToolContext ctxctx = ViewToolContext(getVelocityEngine()requestresponsegetServletContext())ctx.putAll(model)(.getToolboxConfigLocation() != ) { ToolManager tm = ToolManager()tm.setVelocityEngine(getVelocityEngine())tm.configure(getServletContext().getRealPath(getToolboxConfigLocation()))(tm.getToolboxFactory().hasTools(Scope.)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.))} (tm.getToolboxFactory().hasTools(Scope.)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.))} (tm.getToolboxFactory().hasTools(Scope.)) { ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.))} } ctx} }
而我还有一个增加自定义toolbox的需求,
但是像模像样的贴了一个从网上拷贝过来的 toolbox.xml在项目中却发现:
<toolbox> <!-- [ DateTool ] @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/generic/DateTool.html (ja) @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DateTool.html (en) @since VelocityTools 1.0 --> <tool> <key>jsonUtils</key> <scope>application</scope> <class>org.*.JsonUtils</class> </tool> </toolbox>
mvc 跳转到vm中的展示,不识别 jsonUtils,
无奈,跟踪进去
ToolManager tm = ToolManager()
代码发现,原来2.0的 toolbox配置方式 已经换成了另外的形式了。
参考 velocity-tools2.jar/org.apache.velocity.tools.generic.tools.xml 里面的格式,就会发现。原来toolbox的定义方法发生了变化。
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--> <tools> <toolbox scope="application"> <tool class="com.*.utils.VelocityJsonUtils"/> </toolbox></tools>
重新定义一下tools的配置,功能就正常了。
而关于toolbox的名字则用注解的方式
()
解决了
碰到问题网上的解决方案也许比较多的在说明前一个版本如何解决问题,对于自己的问题,需要静下心来看看别人的源码实现,一切就豁然开朗了。
时间: 2024-10-06 04:13:18