这里访问对象的公共领域是我的对象类:如何从Velocity模板
public class Address
{
public final String line1;
public final String town;
public final String postcode;
public Address(final String line1, final String town, final String postcode)
{
this.line1 = line1;
this.town = town;
this.postcode = postcode;
}
}
我把它添加到速度情况下是这样的:
Address theAddress = new Address("123 Fake St", "Springfield", "SP123");
context.put("TheAddress", theAddress);
然而,写作模板时,下面将不会渲染地址字段(但是,当我将getter添加到Address类时它工作正常)
<Address>
<Line1>${TheAddress.line1}</Line1>
<Town>${TheAddress.town}</Town>
<Postcode>${TheAddress.postcode}</Postcode>
</Address>
是否可以从Velocity访问公共字段而不添加getters?
你可以添加任何细节,如链接到文档如何做到这一点或为什么这是必要的? –
这是必要的,因为Velocity不支持公共字段。 试试这个:http://maven-doccheck.sourceforge.net/samples/ShinobuDemo/apidocs/org/apache/velocity/tools/generic/introspection/PublicFieldUberspect.html –
他们很棒,他们按照惯例实现他们的框架而不是实际的语言规则...... – evanmcdonnal