速度事件处理程序 [英] Velocity Eventhandler

查看:22
本文介绍了速度事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在速度方面,当你执行 $object.variable 时,如果它无法找到 getter 函数访问它或 getter 返回空值.它只会在页面上显式显示 $object.variable

in velocity, when you do $object.variable if it not be able to find the getter function to access it or the getter returns a null. it will just show $object.variable explicitly on the page

我知道有一个安静的参考,但我不想添加!对数以千计的变量进行签名.

I know there is a quiet reference, but I don't want to add ! sign to thousands of variables.

我尝试过 InvalidReferenceEventHandler、NullValueHandler,它们都没有被调用.

I have tried InvalidReferenceEventHandler, NullValueHandler they all didn't get called.

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

I wander is there a specific type of Eventhandler for this.

非常感谢

推荐答案

以上似乎也是一个有效的选择.但是这里有另一种选择:

The above seems to be a valid choice as well. However here is another option:

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 文件中:

You'll also need to add the following to your velocity.properties file:

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

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

You might be surprised at the results though, so it will likely need fine-tuning dependent upon your needs.

这篇关于速度事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆