如何配置速度逃逸工具与弹簧属性?

时间:2022-11-24 19:25:27

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 Web应用程序中通过Velocity从模板创建电子邮件。现在我需要HTML转义一些值。我找到了速度逃逸工具。但是我没有让配置工作。

What I have tryed so fare is (spring applicationContext.xml):

我所查询的so fare (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>

Template (htmlEscapeTest.vm):

模板(htmlEscapeTest.vm):

with escape: $esc.html($needEscape)

TestCase:

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;"));
}

But the Test failed, ...got: "with escape: $esc.html($needEscape)"

但是测试失败了,……了:“逃避:$ esc.html(needEscape美元)”

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

有人能告诉我我做错了什么吗?


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

如果我在测试中添加新的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();

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

然后工作。但就我对文档的理解而言,工具应该在属性文件中配置一次。

I am using Velocity Engine 1.7 and Velocity Tools 2.0.

我使用的是Velocity Engine 1.7和Velocity Tools 2.0。

5 个解决方案

#1


6  

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:

您不能直接在VelocityEngine中配置工具。相反,当您使用VelocityEngineUtils时,您可以在模型映射中传递任何工具:

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

Or if you use the VelocityEngine directly you could do:

或者你可以直接使用VelocityEngine:

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

#2


3  

Warning: I'm basing this on somewhat vague memories from a while ago. Mileage may vary.

警告:我是基于一段时间前的模糊记忆。里程可能有所不同。

Some of the Velocity documentation should be read from the perspective of "how do I use this in a VelocityView?" If you want to use the same features directly from java code, then you need to change a few details. In this case, I believe that you're not creating the Context properly. Try to follow the standalone example here, making sure that you "ask [the ToolManager] to create a context for you":

应该从“如何在VelocityView中使用它”的角度来阅读一些Velocity文档。如果您想直接从java代码中使用相同的特性,那么您需要更改一些细节。在这种情况下,我认为您没有正确地创建上下文。试着遵循这里的独立示例,确保“请[工具管理器]为您创建上下文”:

ToolManager manager = ...
Context context = manager.createContext();

Something similar probably is done under the covers for you if you use VelocityView.

如果你使用VelocityView,类似的东西很可能是在你的掩护下做的。

#3


1  

Here's some code that I just got working. I found the standard tools are set up automatically by the ToolManager.

这是我刚开始使用的一些代码。我发现工具管理器会自动设置标准工具。

@Autowired
private VelocityEngine velocityEngine;

public void createHtml(String templateLocation, Map<String, Object> model) throws Exception {
  ToolManager toolManager = new ToolManager();
  ToolContext toolContext = toolManager.createContext();
  VelocityContext velocityContext = new VelocityContext(model, toolContext);
  StringWriter resultWriter = new StringWriter();
  velocityEngine.mergeTemplate(templateLocation, "UTF-8", velocityContext, resultWriter);
  String html = resultWriter.toString();
  // use the HTML here
}

And my template has this

我的模板是这样的

<p>Dear $esc.html($customer.firstname)</p>

#4


0  

  1. Add velocity tool maven dependency in pom or add jar in class path.
  2. 在pom中添加velocity工具maven依赖项,或者在类路径中添加jar。
  3. Add escape tool object in velocity context.

    在velocity上下文中添加escape工具对象。

    [ context.put("escapeTool",new EscapeTool()) ]

    (上下文。把(“escapeTool”,新的escapeTool()))

  4. Use escapeTool in template.

    在模板中使用escapeTool。

    [ $escapeTool.xml(value_to_be_escaped) ]

    [escapeTool.xml美元(value_to_be_escaped))

#5


0  

change this:

改变:

<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>

to:

:

<property name="velocityProperties">
            <value>
                input.encoding=UTF-8
                output.encoding=UTF-8
                tools.toolbox=application
                tools.application.esc=org.apache.velocity.tools.generic.EscapeTool
            </value>
        </property>

#1


6  

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:

您不能直接在VelocityEngine中配置工具。相反,当您使用VelocityEngineUtils时,您可以在模型映射中传递任何工具:

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

Or if you use the VelocityEngine directly you could do:

或者你可以直接使用VelocityEngine:

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

#2


3  

Warning: I'm basing this on somewhat vague memories from a while ago. Mileage may vary.

警告:我是基于一段时间前的模糊记忆。里程可能有所不同。

Some of the Velocity documentation should be read from the perspective of "how do I use this in a VelocityView?" If you want to use the same features directly from java code, then you need to change a few details. In this case, I believe that you're not creating the Context properly. Try to follow the standalone example here, making sure that you "ask [the ToolManager] to create a context for you":

应该从“如何在VelocityView中使用它”的角度来阅读一些Velocity文档。如果您想直接从java代码中使用相同的特性,那么您需要更改一些细节。在这种情况下,我认为您没有正确地创建上下文。试着遵循这里的独立示例,确保“请[工具管理器]为您创建上下文”:

ToolManager manager = ...
Context context = manager.createContext();

Something similar probably is done under the covers for you if you use VelocityView.

如果你使用VelocityView,类似的东西很可能是在你的掩护下做的。

#3


1  

Here's some code that I just got working. I found the standard tools are set up automatically by the ToolManager.

这是我刚开始使用的一些代码。我发现工具管理器会自动设置标准工具。

@Autowired
private VelocityEngine velocityEngine;

public void createHtml(String templateLocation, Map<String, Object> model) throws Exception {
  ToolManager toolManager = new ToolManager();
  ToolContext toolContext = toolManager.createContext();
  VelocityContext velocityContext = new VelocityContext(model, toolContext);
  StringWriter resultWriter = new StringWriter();
  velocityEngine.mergeTemplate(templateLocation, "UTF-8", velocityContext, resultWriter);
  String html = resultWriter.toString();
  // use the HTML here
}

And my template has this

我的模板是这样的

<p>Dear $esc.html($customer.firstname)</p>

#4


0  

  1. Add velocity tool maven dependency in pom or add jar in class path.
  2. 在pom中添加velocity工具maven依赖项,或者在类路径中添加jar。
  3. Add escape tool object in velocity context.

    在velocity上下文中添加escape工具对象。

    [ context.put("escapeTool",new EscapeTool()) ]

    (上下文。把(“escapeTool”,新的escapeTool()))

  4. Use escapeTool in template.

    在模板中使用escapeTool。

    [ $escapeTool.xml(value_to_be_escaped) ]

    [escapeTool.xml美元(value_to_be_escaped))

#5


0  

change this:

改变:

<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>

to:

:

<property name="velocityProperties">
            <value>
                input.encoding=UTF-8
                output.encoding=UTF-8
                tools.toolbox=application
                tools.application.esc=org.apache.velocity.tools.generic.EscapeTool
            </value>
        </property>