Java 如何在Spring 3中使用Velocity工具获取Velocity引擎
如何在Spring 3中使用Velocity工具获得VelocityEngine? 我需要控制器中的一个方法来处理模板Velocity,但需要有可用于初始化Spring3的Velocity工具。 现在我就这样做 弹簧配置:Java 如何在Spring 3中使用Velocity工具获取Velocity引擎,java,spring,velocity,Java,Spring,Velocity,如何在Spring 3中使用Velocity工具获得VelocityEngine? 我需要控制器中的一个方法来处理模板Velocity,但需要有可用于初始化Spring3的Velocity工具。 现在我就这样做 弹簧配置: <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resou
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity/config/toolbox.xml"/>
<property name="viewClass" value="my.tools.VelocityToolsView"/>
</bean>
在方法上:
VelocityContext velocityContext = new VelocityContext(map, toolContext);
StringWriter writer = new StringWriter();
velocityEngine.mergeTemplate("myTeplate.html", "UTF-8", velocityContext, writer);
String templateString = writer.toString();
当您不使用Spring配置时,上述获得速度的方法是很好的。当您使用Spring时,您不需要这么复杂 在spring.xml中定义这个bean
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
在Spring3中有一种更简单的方法 将toolbox.xml添加到WEB-INF/velocity
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<xhtml>true</xhtml>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
<parameter name="format" value="dd/MM/yyyy" />
</tool>
<tool>
<key>display</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DisplayTool</class>
</tool>
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
<tool>
<key>iter</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.IteratorTool</class>
</tool>
<tool>
<key>sort</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.SortTool</class>
</tool>
<tool>
<key>esc</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>
</toolbox>
真的
日期
应用
org.apache.velocity.tools.generic.DateTool
显示
应用
org.apache.velocity.tools.generic.DisplayTool
数学
应用
org.apache.velocity.tools.generic.MathTool
iter
应用
org.apache.velocity.tools.generic.IteratorTool
分类
应用
org.apache.velocity.tools.generic.SortTool
电子稳定控制系统
应用
org.apache.velocity.tools.generic.EscapeTool
然后将此路径添加到APPNAME-servlet.xml文件中
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" p:cache="false" p:order="1">
<property name="prefix" value="/com/aol/dragon/template/"/>
<property name="suffix" value=".vm"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity/toolbox.xml" />
</bean>
然后更新pom.xml依赖项
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
org.apache.velocity
速度工具
2
保存、更新项目并运行服务器。您现在应该能够使用vm中的所有工具。将不可用。它们被添加到类VelocityToolboxView中的上下文中。所以这是行不通的。
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" p:cache="false" p:order="1">
<property name="prefix" value="/com/aol/dragon/template/"/>
<property name="suffix" value=".vm"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity/toolbox.xml" />
</bean>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>