速度事件处理程序

发布于 2024-12-07 21:01:58 字数 259 浏览 0 评论 0原文

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

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

我尝试过 InvalidReferenceEventHandler、NullValueHandler 他们都没有被调用。

我想知道是否有专门类型的事件处理程序用于此目的。

非常感谢

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.

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

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

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-12-14 21:01:58

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

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

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

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 + ">");
  }
}

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.

屋檐 2024-12-14 21:01:58

我基于 Engine-1.7 代码。

似乎当调用无效方法时,会调用实用方法 EventHandlerUtil.invalidGetMethod 。此方法创建一个新的 InvalidGetMethodExecutor (这是 InvalidReferenceEventHandler 上的内部类)。最终,这会链接到对 invalidReferenceHandlerCall 的调用,该调用最终会迭代已定义的任何 handlerIterators。不幸的是,我对 Velocity 的内部了解不够,无法告诉您如何注入这些值。我的猜测是,用户列表将建议一种覆盖此行为的方法,或者建议使用/实现自定义工具。

编辑:

根据开发人员指南,您可以执行以下操作。您需要编写一些代码来处理它,但这应该不会太困难:

Pluggable Introspection

runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

此属性设置“Uberspector”,即处理 Velocity 的所有自省策略的自省包。您可以指定以逗号分隔的 Uberspector 类列表,在这种情况下,所有 Uberspector 都会被链接起来。默认的链接行为是为所有提供的 uberspector 中的每个内省调用返回第一个非空值。您可以通过子类化 org.apache.velocity.util.introspection.AbstractChainableUberspector (或直接实现 org.apache.velocity.util.introspection.ChainableUberspector)来修改此行为(例如限制对某些方法的访问)。这允许您为 Uberspection 创建更有趣的规则或模式,而不仅仅是返回第一个非空值。

I'm basing this off of Engine-1.7 code.

It seems that when an invalid method is called that the utility method EventHandlerUtil.invalidGetMethod is called. This method creates a new InvalidGetMethodExecutor (this is an inner class on InvalidReferenceEventHandler). Eventually this chains down into a call to invalidReferenceHandlerCall which eventually iterates over any handlerIterators which have been defined. Unfortunately I don't know enough about the internals of Velocity to tell you how to inject these values though. My guess is that the user list will suggest a way to override this behavior or a suggestion will be to use / implement a custom tool.

Edit:

According to the Developer Guide you can do the following. You'll need to write some code to deal with it, but it shouldn't be too difficult:

Pluggable Introspection

runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

This property sets the 'Uberspector', the introspection package that handles all introspection strategies for Velocity. You can specify a comma-separated list of Uberspector classes, in which case all Uberspectors are chained. The default chaining behaviour is to return the first non-null value for each introspection call among all provided uberspectors. You can modify this behaviour (for instance to restrict access to some methods) by subclassing org.apache.velocity.util.introspection.AbstractChainableUberspector (or implementing directly org.apache.velocity.util.introspection.ChainableUberspector). This allows you to create more interesting rules or patterns for Uberspection, rather than just returning the first non-null value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文