Apache Velocity Quiet Reference Notation 作为默认行为

cae*_*cae 5 java velocity template-engine

当 Velocity 遇到未定义的引用时,它的正常行为是输出引用的图像。例如,假设以下引用作为 VTL 模板的一部分出现”示例:

<input type="text" name="email" value="$!email"/>
Run Code Online (Sandbox Code Playgroud)

是否可以配置默认行为,以便我仍然可以编写$email并且它隐含地是一个安静的符号?

Wil*_*ass 2

是的。当发现无效引用时,会调用一个事件处理程序。它称为 InvalidReferenceEventHandler。您需要让事件处理程序返回一个空字符串。

public Object invalidGetMethod( Context context, 
                                    String reference, 
                                    Object object, 
                                    String property, 
                                    Info info)
{
    return "";
}
Run Code Online (Sandbox Code Playgroud)

有关如何创建和注册事件处理程序的详细信息,请参阅 Velocity 开发人员指南。但简而言之,实现 InvalidReferenceEventHandler,然后在设置 VelocityEngine 时包含此属性。

eventhandler.invalidreferences.class=com.something.youreventhandlerclass
Run Code Online (Sandbox Code Playgroud)