将 Velocity 工具与 Spring 3.0.3 结合使用
当我更新 bean 时:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<property name="toolboxConfigLocation" value="tools.xml" />
</bean>
使用 Velocity Tools 的 tools.xml 路径,我得到:
Caused by:
java.lang.ClassNotFoundException: org.apache.velocity.tools.view.ToolboxManager
我尝试插入工具版本 2 和 1.4,但都没有此包结构。我错过了一些明显的事情吗? Spring/Velocity 组件支持什么版本的 Velocity Tools?
When I update the bean:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<property name="toolboxConfigLocation" value="tools.xml" />
</bean>
With the tools.xml path for Velocity Tools, I get:
Caused by:
java.lang.ClassNotFoundException: org.apache.velocity.tools.view.ToolboxManager
I've tried plugging in tools version 2 and 1.4, neither have this package structure. Did I miss something obvious? What version of Velocity Tools is the Spring/Velocity component supporting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我用的是稍微简单一点的方法。由于缺乏配置文档和示例,我也无法强制 Velocity Tools 工作。我只是获取velocity-generic-tools-2.0.jar 并在视图解析器中进行一些更改:
然后,在velocity 模板中,您可以像往常一样使用它$esc.html($htmlCodeVar)。这个解决方案非常简单,没有大量的配置和重写 spring 类。
I use a little bit simpler of a way. I also cannot force Velocity Tools to work due to lack of configuration documentation and examples. I just get the velocity-generic-tools-2.0.jar and make a little change in my view resolver:
Then, in the velocity template you can use it as usual $esc.html($htmlCodeVar). This solution is very simple, without tons of configs and overriding spring classes.
Spring 默认情况下具有非常过时的 Velocity 支持。我从 Spring 扩展了 VelocityView 类,并重写了我自己初始化 Tools 的 createVelocityContext 方法。 这里是它的样子结束。
Spring has very outdated Velocity support by default. I extend
VelocityView
class from Spring and overridecreateVelocityContext
method where I initialize Tools myself. Here is how it looks at the end.在 3.0.5 中,我使用了与 serg 发布的类似的类,唯一的修改是使用 spring 未使用的更新的类(通过 VelocityToolboxView -> ServletToolboxManager 的尾部(在我们已覆盖的 createVelocityContext 中使用)这就是该类它已被弃用,所以我将 serg 的答案中的 initVelocityToolContext 修改为:
我还必须更改创建 VelocityContext 的行以显然调用此方法,
我的 bean 现在看起来像:
With 3.0.5 I used a similar class to what serg posted, with the only modification being to use the updated classes which spring did not use (tail through VelocityToolboxView -> ServletToolboxManager (used in the createVelocityContext we have overridden) That is the class which is deprecated, so I modified the initVelocityToolContext in serg's answer to be:
I also had to change the line which created the VelocityContext to call this method obviously.
My bean now looks like:
受到 Scott 和 serg 答案的启发,这是另一种不需要 XML 的方法: http://squirrel.pl/blog/2012/07/13/spring-velocity-tools-no-xml/
Inspired by answers from Scott and serg, here's another way to do it that does not require XML: http://squirrel.pl/blog/2012/07/13/spring-velocity-tools-no-xml/
受到上面所有答案的启发,这是我对 spring 和velocity-tools 2.0 的
VelocityLayoutView
的实现,添加了一些改进!Inspired by all the answers above, this is my implementation of
VelocityLayoutView
for spring and velocity-tools 2.0, added some improvement!我发现 @serg 技术的这种变体对我有用。
I found that this variation on @serg's technique worked for me.