当尝试使用java源代码作为Velocity的模板时,它会在模板的这一行崩溃:
/* @see panama.form.Validator#validate(java.lang.Object) */
有这个例外:
Exception in thread "main" org.apache.velocity.exception.ParseErrorException: Lexical error, Encountered: "l" (108), after : "." at *unset*[line 23, column 53]
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1301)
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1265)
at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:199)
显然,当它试图解析宏的参数时,它需要#validate
一个宏并崩溃。有什么可以做的吗?
我正在使用Velocity 1.7。
修改
我知道我可以逃避模板文件中的#
字符,但是有很多字符也可能偶尔会改变,所以我更喜欢一种不需要手动更改的方法。文件。
答案 0 :(得分:2)
第一个选项 从这里尝试此解决方案:Escaping VTL Directives
VTL指令可以使用反斜杠字符(" \")以类似于有效VTL引用的方式进行转义。
## #include( "a.txt" ) renders as <contents of a.txt>
#include( "a.txt" )
## \#include( "a.txt" ) renders as #include( "a.txt" )
\#include( "a.txt" )
## \\#include ( "a.txt" ) renders as \<contents of a.txt>
\\#include ( "a.txt" )
第二个选项
你有这个工具[EscapeTool][2]
。
用于在Velocity模板中进行转义的工具。
它为转义Java 输出,JavaScript,HTML,XML和SQL提供了方法。 还提供了渲染VTL字符的方法,否则需要转义。
第三个选项: 您也可以尝试这种解决方法,我没有使用它,但它应该可以工作:
您可以在开始时将模板读作String
,然后预解析。例如,将所有#
替换为\#
,或添加到文件的开头
#set( $H = '#' )
$H$H
请参阅此答案:How to escape a # in velocity然后使用此答案从预先解析的 String
创建模板:How to use String as Velocity Template?