Velocity模板语言:如何导入EscapeTool

问题描述 投票:1回答:1

我正在尝试将unurl用于我的映射模板。

  $url                         -> hello here & there
  $esc.url($url)               -> hello+here+%26+there
  $esc.unurl($esc.url($url))   -> hello here & there

我编写了以下映射模板,但是$esc.unurl([...])不起作用。我不知道如何解决它。原因之一可能是我缺少导入,但是我不知道如何正确导入EscapeTool。

#set($httpPost = $input.path('$').split("&"))
{
#foreach( $kvPair in $httpPost )
 #set($kvTokenised = $kvPair.split("="))
 #if( $kvTokenised.size() > 1 )
   "$kvTokenised[0]" : "$esc.unurl($kvTokenised[1])"#if( $foreach.hasNext ),#end
 #else
   "$kvTokenised[0]" : ""#if( $foreach.hasNext ),#end
 #end
#end
}
aws-api-gateway velocity apache-velocity
1个回答
1
投票

您需要将其添加到tools.xml

示例tools.xml配置(如果要与VelocityView一起使用):

<tools>
  <toolbox scope="application">
    <tool class="org.apache.velocity.tools.generic.EscapeTool"/>
  </toolbox>
</tools>

或使用code在速度上下文中添加它:

ModelMap model = new ModelMap();
model.put("esc", new EscapeTool());
VelocityEngineUtils.mergeTemplateIntoString(
            velocityEngine, "template.vm", "UTF-8", model)
© www.soinside.com 2019 - 2024. All rights reserved.