【问题标题】:Can't explain velocity's behavior无法解释速度的行为
【发布时间】:2013-07-25 08:52:59
【问题描述】:

有速度模板像

#foreach($item in $items)
<tr>
    <td>$item.getClass() $item.getKey()</td>
    <td>$item.summary $item.Summary $item.getSummary()</td>
</tr>
#end

Velocity manual sais,我可以访问 $item 字段作为 getter -“getSummary()”。

我将 ReportRow 项目的 ArrayList 传递给变量“项目”,并希望速度绘制项目摘要。 ReportRow 是一个简单的 POJO 类:

public class ReportRow {
    private String key;
    private String summary;

    public ReportRow(String key, String summary) {
        this.key = key;
        this.summary = summary;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    private String getSummary() {
        return summary;
    }

    private void setSummary(String summary) {
        this.summary = summary;
    }

    @Override
    public String toString() {
        return key + " " + summary;
    }
}

当 Velocity 渲染文本时,我得到:

  • 对于第一列:“class ...ReportRow MyKey” - 没关系 - $item 包含我的对象,并且它的 Key 字段是可访问的;
  • 对于第二列:“$item.summary $item.Summary $item.getSummary()” - 为什么

将项目放入上下文时,所有字段都不为空。

我错过了什么?

【问题讨论】:

    标签: java velocity


    【解决方案1】:

    因为 getter 是 private。出于明显的安全原因,Velocity 不允许从模板调用私有方法。只需使用方法public 即可使其正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多