【问题标题】:Velocity: Iteration issue速度:迭代问题
【发布时间】:2015-08-07 00:53:36
【问题描述】:

之后,从 1.5 升级到速度引擎 1.7 时出现了 1.5 不存在的问题。为了解释这个问题,我必须显示一个代码 sn-p:

#foreach($someVariable in $someCollection)
 #foreach($anotherVariable in $someVariable.$anotherCollection)
   $anotherVariable.someAttribute ## This expression print in the browser as is
   ## but if I do this way
   $anotherVariable.get("someAttribute") ## works fine!
 #end
#end

这发生在升级之后(在1.7),如果我回滚升级(移回1.5),那么我不必以我在上面代码中提到的其他方式sn- p.

【问题讨论】:

  • $anotherVariable的类型是什么?
  • @EvanHaas 它是通用 ArrayList
  • ArrayList??那么 someAttribute 是什么?
  • @NathanBubna ArrayList 持有自定义类作为项目
  • java.util.ArrayList's get method 仅采用整数索引。这是您自己的 ArrayList 自定义扩展吗?

标签: java velocity


【解决方案1】:

尝试将以下内容添加到您的 MyClass extends HashMap:

公共对象获取(对象键){ 返回获取((字符串)键); }

我担心 Velocity 可能会识别出它是一个 Map 并尝试使用不承认泛型的快捷方式,从而调用错误的 get() 方法。但是我没有准备好在这台机器上确认这一点,我也没有时间这样做。对不起。

【讨论】:

    【解决方案2】:

    您可能需要检查您的 .java 文件。

    #foreach($someVariable in $someCollection)
        #foreach($anotherVariable in $someVariable.$anotherCollection)
            $anotherVariable.someAttribute 
            $anotherVariable.get("someAttribute") 
        #end
    #end
    

    假设 someVariable 是 X 类的对象。 对于您的 someVariable,应该有一个公共 get() 方法来获取类 X 中的 anotherCollection; 同样,假设 anotherVariable 是类 Y 的对象。 对于您的另一个变量,应该有一个公共 get() 方法来获取类 Y 中的 someAttribute。

    这适用于我们尝试访问 someAttribute 的两种方式,如上所示。

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多