background image

Defining Functions

<< Using Functions | JavaBeans Components >>
<< Using Functions | JavaBeans Components >>

Defining Functions

<c:when
test=
"${f:equals(selectedLocaleString,
localeString)}
" >
In this example, the expression referencing the function is using immediate evaluation syntax.
A page author can also use deferred evaluation syntax to reference a function in an expression,
assuming that the attribute that is referencing the function can accept deferred expressions.
If an attribute references a function with a deferred expression then the function is not invoked
immediately; rather, it is invoked whenever the underlying technology using the function
determines it should be invoked.
Defining Functions
To define a function, program it as a public static method in a public class. The
mypkg.MyLocales
class in the date example defines a function that tests the equality of two
String
s as follows:
package mypkg;
public class MyLocales {
...
public static boolean equals( String l1, String l2 ) {
return l1.equals(l2);
}
}
Then map the function name as used in the EL expression to the defining class and function
signature in a TLD (see
Chapter 8, "Custom Tags in JSP Pages"
). The following functions.tld
file in the date example maps the equals function to the class containing the implementation of
the function equals and the signature of the function:
<function>
<name>equals</name>
<function-class>mypkg.MyLocales</function-class>
<function-signature>boolean equals( java.lang.String,
java.lang.String )</function-signature>
</function>
No two functions within a tag library can have the same name.
Unified Expression Language
The Java EE 5 Tutorial · September 2007
166