這裏訪問對象的公共領域是我的對象類:如何從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