【问题标题】:How to configure Velocity Escape Tool with Spring Properties?如何使用 Spring Properties 配置 Velocity Escape Tool?
【发布时间】:2016-10-12 16:20:21
【问题描述】:

我在 Spring Web 应用程序中通过 Velocity 从模板创建电子邮件。现在我需要对一些值进行 HTML 转义。我找到了速度Escape Tool。但我没有让配置工作。

我尝试过的是(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):

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

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

谁能告诉我我做错了什么?


如果我在测试中添加new EscapeTool()explicite:

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。

【问题讨论】:

标签: java spring velocity


【解决方案1】:

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

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

或者,如果您直接使用 VelocityEngine,您可以这样做:

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

【讨论】:

    【解决方案2】:

    警告:我是根据不久前的一些模糊记忆得出的。里程可能会有所不同。

    应该从“我如何在VelocityView 中使用它?”的角度来阅读一些 Velocity 文档。如果您想直接从 java 代码中使用相同的功能,那么您需要更改一些细节。在这种情况下,我相信您没有正确创建Context。尝试关注the standalone example here,确保您“要求 [the ToolManager] 为您创建上下文”:

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

    如果您使用VelocityView,可能会在幕后为您完成类似的事情。

    【讨论】:

    • @jtoberon 我遇到了类似的问题。我的理解是VelocityEngineFactoryBean 处理了其中的一些,但我不太清楚到底有多少。
    • 嗯,这对 Spring 特别有意义,他说他正在使用 Spring。然而他的例子不起作用。弄清楚这一点可能需要在调试器中逐步完成失败的测试,以准确检查 VelocityEngineUtils 运行时上下文中的内容。
    【解决方案3】:

    这是我刚刚开始工作的一些代码。我发现标准工具是由 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
    }
    

    我的模板有这个

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

    【讨论】:

      【解决方案4】:
      1. 在 pom 中添加速度工具 maven 依赖项或在类路径中添加 jar。
      2. 在速度上下文中添加逃生工具对象。

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

      3. 在模板中使用 escapeTool。

        [$escapeTool.xml(value_to_be_escaped)]

      【讨论】:

        【解决方案5】:

        改变这个:

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

        到:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-07
          • 1970-01-01
          • 1970-01-01
          • 2010-11-23
          • 2019-01-24
          • 2018-06-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多