本文介绍了如何从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;
    }
}

我将它添加到速度上下文中,如下所示:

I add it to the velocity context like this:

Address theAddress = new Address("123 Fake St", "Springfield", "SP123");
context.put("TheAddress", theAddress);

但是,在编写模板时,以下内容不会呈现地址字段(但是,它工作正常当我将getter添加到Address类时)

However, when writing the template, the following will not render the address fields (however, it works fine when I add getters to the Address class)

<Address>
    <Line1>${TheAddress.line1}</Line1>
    <Town>${TheAddress.town}</Town>
    <Postcode>${TheAddress.postcode}</Postcode>
</Address>

是否可以在不添加getter的情况下访问Velocity对象的公共字段?

Is it possible to access public fields on objects from Velocity without adding getters?

推荐答案

不是默认的。您需要配置不同的Uberspect实现。

Not by default. You need to configure a different Uberspect implementation.

这篇关于如何从Velocity模板访问对象的公共字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 05:43