gpt4 book ai didi

java - Velocity 可以处理对象还是只能处理变量?

转载 作者:行者123 更新时间:2023-12-01 23:45:53 25 4
gpt4 key购买 nike

我现在正在使用速度模板,我想知道是否可以传入 Customer 对象,或者是否需要独立传入值?

例如,我可以做 $customer.name$customer.title 等,还是需要继续做 $name >、$title 等?谢谢!

最佳答案

查看 Velocity user guide ,有一个关于方法的部分,其中显示了调用对象方法的不同方法:

Methods

A method is defined in the Java code and is capable of doing something useful, like running a calculation or arriving at a decision. Methods are references that consist of a leading "$" character followed a VTL Identifier, followed by a VTL Method Body. A VTL Method Body consists of a VTL Identifier followed by an left parenthesis character ("("), followed by an optional parameter list, followed by right parenthesis character (")"). These are examples of valid method references in the VTL:

$customer.getAddress() $purchase.getTotal() $page.setTitle( "My Home Page" ) $person.setAttributes( ["Strange", "Weird", "Excited"] )

接下来是关于引用对象属性的部分:

Property Lookup Rules

As was mentioned earlier, properties often refer to methods of the parent object. Velocity is quite clever when figuring out which method corresponds to a requested property. It tries out different alternatives based on several established naming conventions. The exact lookup sequence depends on whether or not the property name starts with an upper-case letter. For lower-case names, such as $customer.address, the sequence is

getaddress()
getAddress()
get("address")
isAddress()

For upper-case property names like $customer.Address, it is slightly different:

getAddress()
getaddress()
get("Address")
isAddress()

所以是的,您可以将对象放入 Velocity 上下文中并引用它们的字段。

关于java - Velocity 可以处理对象还是只能处理变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17067500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com