速度事件处理器

标签 velocity

在 velocity 中,当你执行 $object.variable 时,如果它找不到 getter 函数 访问它或 getter 返回 null。它只会在页面上明确显示 $object.variable

我知道有一个安静的引用,但我不想添加!签署数千个变量。

我已经尝试过 InvalidReferenceEventHandler、NullValueHandler 它们都没有被调用。

我想知道是否有特定类型的事件处理程序。

非常感谢

最佳答案

以上似乎也是一个有效的选择。然而,这是另一种选择:

public class AppSpecificInvalidReferenceEventHandler implements 
                                                     InvalidReferenceEventHandler
{

  private static final Logger LOGGER = 
     Logger.getLogger(AppSpecificInvalidReferenceEventHandler.class);

  @Override
  public Object invalidGetMethod(Context context, String reference, 
                                 Object object, String property, Info info)
  {
    reportInvalidReference(reference, info);
    return "";
  }

  @Override
  public boolean invalidSetMethod(Context context, String leftreference, 
                                  String rightreference, Info info)
  {
    reportInvalidReference(leftreference, info);
    return false;
  }

  @Override
  public Object invalidMethod(Context context, String reference, Object object, 
                              String method, Info info)
  {
    if (reference == null) {
      reportInvalidReference(object.getClass().getName() + "." + method, info);
    } else {
      reportInvalidReference(reference, info);
    }
    return "";
  }

  private void reportInvalidReference(String reference, Info info)
  {
    LOGGER.info("REFRERENCE: " + reference + " Info <" + info + ">");
  }
}

您还需要将以下内容添加到您的 velocity.properties 文件中:

eventhandler.invalidreferences.class=path.to.package.AppSpecificInvalidReferenceEventHandler,org.apache.velocity.app.event.implement.ReportInvalidReferences

您可能会对结果感到惊讶,因此可能需要根据您的需要进行微调。

关于速度事件处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649289/

相关文章:

javascript - 速度.js : scroll element's x-axis

java - Apache Velocity 自定义 Uberspect

spring - 检查Apache Velocity中的映射是否为空

apache - 构建失败 - Apache Parquet-MR 源(mvn 安装失败)

java - Spring MVC + 速度 : proper way to bind DropDown options

java - 自定义添加应用程序链接不起作用

spring - 在 Spring Boot 中禁用 Velocity View 解析器

java - 在 jar 文件中加载速度模板

java - Velocity 2 不会设置属性

java - MySQL 数据库连接速度无法正常工作