How to print a list of objects in a Velocity templ

2019-08-08 06:31发布

This is a pretty basic problem and I'm pretty sure I'm doing something wrong or making some assumption. Here goes.

I'm writing a Jira plugin, which uses the Velocity template system. I have a list of ResultRow objects where ResultRow is a class with a single member variable: String key:

public class ResultRow {
  public String key;
}

I have a list of these ResultRows:

List<ResultRow> rows = new ArrayList<ResultRow>();

ResultRow row = new ResultRow();
row.key = "foo";

rows.add(foo);

Map<String, Object> velocityParams = new HashMap<String, Object>();
velocityParams.put("rows", rows);

return descriptor.getHtml("view", velocityParams);

and I am trying to list these rows in a template with the following:

#foreach ($row in $rows)
  <tr><td>$row.key</td></tr>
#end

I want the output to be: foo. Maddeningly, the template system simply prints the literal string "$row.key" instead of the contents of key. To verify that "$row" is indeed an object, I used the template:

#foreach ($row in $rows)
  <tr><td>$row</td></tr>
#end

and the result was as expected: com.domain.jira.ResultRow@7933f2c6.

I think maybe I'm missing some requirement for the class. Does it need to be defined in some special way to suggest to Velocity that certain members are usable in templates? Does Jira use some special funky version of Velocity that only works with certain objects?

标签: jira velocity
2条回答
干净又极端
2楼-- · 2019-08-08 07:08

I guess the answer is you cannot do what I was trying to do. You can call member methods but you can't access member variables, which means you'll need to add getters to your class. (Could've sworn I tried that. Ah well.)

查看更多
干净又极端
3楼-- · 2019-08-08 07:10

Velocity does not expose fields, only methods. There are ways to change that:

  • You can create your own Uberspect class that allows access to public fields.

  • You can wrap the instance with a modified version of Velocity's FieldMethodizer that gives access to non-static fields.

  • You can add and use an instance of a "tool" class to your context, such as a subclass of VelocityTool's ClassTool.

查看更多
登录 后发表回答