To support Java beans, SportPage adds two context utility objects, $params and $javabeans:
$params.setProperties( $mybean )
The bean is created according to the following method search sequence:
getInstance(ExtendedProperties) method which has access to the server configuration.
if the singleton method fails, it will then try for a null constructor, the standard constructor for java beans. In this way, any class that can be included in JSP using the usebean tag can be included under a request scope in a Velocity page; if the need arises (and it probably will) this will be expanded to allow constructors to instance a bean with page and/or webapp scope.[3]
Inside a Velocity template, we can use constructs like
#set ($now = $javabean.getBean("java.util.Date"))
or, if the bean supports it, have a form handler selecting weather cities and use
#set ($weather = $javabean.getBean($context, "cbc.utilities.weather" )) $params.setProperties( $weather )
default.weather.bean=cbc.utilities.weather
will define a global $weather variable using the specified class
sports.luge.history.weather.bean=cbc.utilities.weather_history
would mean that, in sports/luge/history only, the $weather refers to a different class of object.
![]() | Caution |
---|---|
Because dot-variable and BeanFactor constructors are evaluated as each template is accessed, they are expensive to invoke; the beans should cache themselves or be singleton objects where ever practical, and constructors should be lightening quick. |
[3] Java beans can also be created under an applications scope by attaching bean instances to the $global application configuration Map object. Because Velocity templates are all, to Java, the same servlet, there is no equivalent to a page scope without extra programming to cache the object based on the $path; examples of such caching can be found in the JDOMFile source code.