速度为正常字符串中存在的连字符提供解析异常

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

对于字符串

#TestString (test-string this is random string)
,速度引擎在评估时会抛出解析异常。

org.apache.velocity.exception.ParseErrorException: Encountered "-" at PERSONALISATION[line 1, column 18]
Was expecting one of:
    <RPAREN> ...
    <WHITESPACE> ...
    <WHITESPACE> ...

正在使用Velocity 1.7,评估时传递的上下文除了Escape Tool之外都是空的。

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("esc", new EscapeTool());
StringWriter writer = new StringWriter();
getVelocityEngine().evaluate(velocityContext, writer, "TEST-TEMPLATE", text); 
java templates velocity apache-velocity
1个回答
0
投票

在 Velocity 中,指令以

#
开头,如
#if
#foreach
等。行首的
#
使 Velocity 认为
#TestString
是指令,因此在遇到左括号后,它正在寻找右括号并遇到
-
。因此出现解析错误。像
#TestString(test)
#TestString('test-string this is random string')
TestString#
这样的东西不会导致任何解析错误。如果您渲染的字符串需要看起来与
#TestString (test-string this is random string)
完全相同,您可以使用如下所示的内容:

#set($hash = '#')
${hash}TestString (test-string this is random string)
© www.soinside.com 2019 - 2024. All rights reserved.