如何使用Spring Properties配置Velocity Escape Tool? [英] How to configure Velocity Escape Tool with Spring Properties?

查看:117
本文介绍了如何使用Spring Properties配置Velocity Escape Tool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Web应用程序中通过Velocity从模板创建电子邮件.现在,我需要HTML转义某些值.我发现了Velocity 转义工具.但是我没有使配置生效.

I create e-mails from templates via Velocity in a Spring Web Application. Now I need to HTML escape SOME of the values. I found the Velocity Escape Tool. But I did not get the configuration working.

我到目前为止尝试过的是(spring applicationContext.xml):

What I have tryed so fare is (spring applicationContext.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>

模板(htmlEscapeTest.vm):

Template (htmlEscapeTest.vm):

with escape: $esc.html($needEscape)

TestCase:

@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("&lt;test&gt;"));
}

但是测试失败,...got: "with escape: $esc.html($needEscape)"

有人能给我一个提示我做错了什么吗?

Can anybody give me a hint what I am doing wrong?

如果我在测试中添加new EscapeTool()显式信息:

If I add new EscapeTool() explicite in the test:

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();

然后它正在工作.但是据我了解的文档,这些工具应该在属性文件中配置一次.

then it is working. But as far as I understand the documentation, the tools should be configured once in the properties file.

我正在使用Velocity Engine 1.7和Velocity Tools 2.0.

I am using Velocity Engine 1.7 and Velocity Tools 2.0.

推荐答案

您不能直接在VelocityEngine中配置工具.相反,您要做的是,当您使用VelocityEngineUtils时,您会在模型图中传递任何工具:

You can not configure the Tools directly in the VelocityEngine. What you do instead, is that when you use the VelocityEngineUtils that you pass any Tools within the model map:

ModelMap model = new ModelMap();
model.put("esc", new EscapeTool());
VelocityEngineUtils.mergeTemplateIntoString(
                velocityEngine, "template.vm", "UTF-8", model)

或者,如果直接使用VelocityEngine,则可以:

Or if you use the VelocityEngine directly you could do:

VelocityContext velocityContext = new VelocityContext(model);
velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer);

这篇关于如何使用Spring Properties配置Velocity Escape Tool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆