Velocity Template Language: How to import EscapeTool

自闭症网瘾萝莉.ら 提交于 2020-01-06 04:47:04

问题


I am trying to use unurl for my mapping template.

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

I wrote the following mapping template, but $esc.unurl([...]) doesn't work. I couldn't figure out how to fix it. One reason might be that I am missing an import, but I don't know how to import the EscapeTool properly.

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

回答1:


You need to add it to tools.xml

Example tools.xml config (if you want to use this with VelocityView):

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

Or add it in velocity context using code:

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


来源:https://stackoverflow.com/questions/59440132/velocity-template-language-how-to-import-escapetool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!