如何使用弹簧属性配置速度逃逸工具?
我通过 Spring Web 应用程序中的 Velocity 从模板创建电子邮件。现在我需要HTML转义一些值。我找到了速度逃逸工具。但是我没有让配置工作。
我尝试过的是(弹簧应用程序context.xml):
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="classpath:/velocity/emailTemplates" />
<property name="preferFileSystemAccess" value="false" />
<property name="overrideLogging" value="true" />
<property name="velocityProperties">
<util:properties>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="tools.toolbox">application</prop>
<prop key="tools.application.esc">org.apache.velocity.tools.generic.EscapeTool</prop>
</util:properties>
</property>
</bean>
Template (htmlEscapeTest.vm):
with escape: $esc.html($needEscape)
测试用例:
@Test
public void testHtmlEscapingSupport() {
final String needEscape = "<test>";
ModelMap model = new ModelMap();
model.addAttribute("needEscape", needEscape);
String result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, HTML_ESCAPING_TEMPLATE_FILE, model);
assertThat(result, StringContains.containsString("<test>"));
}
但测试失败了,...got: "with escape: $esc.html($needEscape)"
任何人都可以给我一个提示,我做错了什么吗?
如果我在测试中添加显式:new EscapeTool()
VelocityContext velocityContext = new VelocityContext(model);
velocityContext.put("esc", new EscapeTool());
StringWriter writer = new StringWriter();
velocityEngine.mergeTemplate(HTML_ESCAPING_TEMPLATE_FILE, velocityContext, writer);
String result = writer.toString();
然后它正在工作。但据我所知,这些工具应该在属性文件中配置一次。
我正在使用Velocity Engine 1.7和Velocity Tools 2.0。