Why ?
1. New Keywords
1.1. $NewObject
1.2. $Invoke
2. How does it work ?
3. Multiple pages objects
4. Extra Possibilities
5. Examples
$NewObject is able to call a constructor to create new Java object, you can pass parameter to the constructor method. This keyword returns a object which is stored for the current template.$NewObject object $classname(param*)$with:
object: is the name of the object locally created, the type of the object is 'classname'
classname: is the name of the class of the object (e.g.: java.lang.String)
param: is a parameter of the class constructor, if there is no parameter for the current constructor, you only have to open and close brackets. In case, there are multiple parameters, you have to separate parameters with pipe '|'. param is composed by:
(type)paramNamewith:
type: is the type of the parameter (e.g. int or java.lang.String, etc...)
paramName: is the name of the parameter or its value
$Invoke invoques a method on a specific Java object. It returns something when it is possible.$Invoke result $object.method(param*)$with:
result: is the result of method call
object: is the name of the object previously created
method: is the name of the method that must be called
param: is a parameter of the method, if there is no parameter for the current method, you only have to open and close brackets. In case, there are multiple parameters, you have to separate parameters with pipe '|'. param is composed by:
(type)paramNamewith:
type: is the type of the parameter (e.g. int or java.lang.String, etc...)
paramName: is the name of the parameter or its value
The $Invoke keyword is able to return :
3. Multiple pages objects
You can create objects which live into multiple tmpl pages. To use this possbility, you need
to use a special word on $NewObject or $Invoke keywords.
$NewObject isPersist test $String((String)$myVar$)$or
$Invoke isPersist res $test.length()$Then, test or res objects could be used in others pages, but in the same client session.
$CloseObject closes a Java object. After this call, you could not use anymore the object.$CloseObject objectwith:
object: is the name of the object you want to delete and close
$DefVar myVar thisIsATest $NewObject test $String((String)$myVar$)$o When a variable or object is not defined, and you try to use it, the String representation of this String is used to build the object.
5. Examples
The following example creates a String object with a simple String, and calls the length() method.
In this example we can directly display the length without any other operation:
$NewObject test $java.lang.String((java.lang.String)"this is a string")$ $Invoke res $test.length()$ The length is: $res$The following example creates a String object with a short name object. Only for String object, you can use this possibility:
$NewObject test2 $String((String)"this is a string")$ $Invoke res2 $test2.length()$ The length is: $res2$The following example creates a String object with a simple String, without any cast (you can do that only with String object):
$NewObject test3 $String("tototer")$ $Invoke res3 $test3.length()$ The length is: $res3$The following example creates a String object with a previous created object (test2). Java Objects use do not use $$ syntax. $$ syntax is only used for scripting variables:
$NewObject test4 $String(test2)$ $Invoke res4 $test4.length()$ The length is: $res4$The following example creates a String object with a previous create object (test3) with a cast:
$NewObject test5 $String((String)test3)$ $Invoke res5 $test5.length()$ The length is: $res5$The following example creates a TesterCollection object with a single int parameter (3). In this case, we need to cast the value, to be sure to use the properly Java type. This object has 2 methods, a getVector method which returns a Vector object (Collection), and a getArrayList method which returns a ArrayList object (Collection):
$NewObject tester $TesterCollection((int)3)$ $Invoke col $tester.getVector()$ $Invoke col2 $tester.getArrayList()$Now we can loop on results on col and col2 objects:
$LoopOnResults col cc $cc:value$.<br> $EndLoop $LoopOnResults col2 cc2 $cc2:value$.<br> $EndLoopThe following example uses the previous created object (tester object) to get a ResultSet from a database. A rset object is used with $LoopOnResults keyword to loop on datas.
$Invoke rset $tester.getresultSet()$ $LoopOnResults rset rr $rr:id$ - $rr:foo$.<br> $EndLoopThe following example creates a String object with a previous defined variable (and with a cast (mandatory)):
$DefVar myVar thisIsATest $NewObject atest $String((String)$myVar$)$ $Invoke resATest $atest.length()$ The length is: $resATest$