A method is part of a context object, written in Java code, and is capable of doing something useful, like running a calculation or arriving at a decision. Method invocation is indicated by round parentheses following the method name. These parentheses can optionally contain a parameter list.
Example 4.3. Valid method references
$cart.calculateTotal() $customer.updatePurchases($cart) $shop.checkInventory() $cart.addTaxes(8, 16) # Property access $page.setTitle( "My Home Page" ) $customer.getAddress() $purchase.getTotal()
The last few examples are similar to the examples seen earlier when discussing properties. As stated there, the properties notation is a short-hand notation for the invocation of bean getters and setters.[6]
The main difference between Properties and Methods is that you can specify a parameter list to a Method.
Example 4.4. Methods that can be used with shorthand notation
$shop.getFruits() $sun.getPlanets() $album.get("logo") $shop.setCustomer($customer) $sun.setPlanetCount(9) $album.put("logo", "newlogo") ## equal property getters and setters $shop.fruits $sun.Planets $album.logo #set ($shop.customer = $customer) #set ($sun.planetCount = 9) #set ($album.logo = "New Logo")
![]() | Warning |
---|---|
The last |
Not every method invocation can be replaced by short hand
notation. Even if a method is called
set<property>()
or
get<property>()
, it must still adhere to bean
getter and setter method signatures.
Example 4.5. Methods that can not be used with shorthand notation
## Can't pass a parameter with $sun.planet $sun.getPlanet(3) ## Velocity only allows shorthand notation for getter and setter $shop.orderFruits() ## Can't pass a parameter list $book.setAuthorAndTitle("George Orwell", "Homage to Catalonia")
[6] By explicitly stating the method name and parameters, the
Uberspector is not used. This is useful if you object has both,
get(String)
and explicit property getter
methods.