【问题标题】:Velocity Eventhandler速度事件处理程序
【发布时间】:2011-10-05 22:23:38
【问题描述】:

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

我知道有一个安静的参考,但我不想添加!签署数以千计的变量。

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

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

非常感谢

【问题讨论】:

    标签: velocity


    【解决方案1】:

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

    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
    

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

    【讨论】:

    • 还是不行,scott,我还在努力寻找答案,会更新的。
    • 您在上面的代码中看到了什么行为?我在我们的应用程序中对其进行了测试,并且确实在渲染页面上没有显示无效引用。你确定你的财产被拿走了。向示例中添加任何日志记录?
    【解决方案2】:

    我基于 Engine-1.7 代码。

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

    编辑:

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

    可插拔内省

    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 创建更有趣的规则或模式,而不仅仅是返回第一个非空值。

    【讨论】:

    • 在我的大多数情况下,getter 本身是有效的,但是 getter 返回的值可能为 null,这是难点,因为方法是有效的,因此永远不会调用无效的 st ethos跨度>
    • 如果引用为 null,则基本上遍历相同的代码(当 getter 不存在时,执行的调用返回 null -- 除非您打开了严格引用)。
    • 如何开启严格引用?你的意思是不使用安静的参考?但仍然非常感谢您的时间和善意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2013-12-24
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多