Working With Functions in Expression Language


Working with EL Functions:

This article is next in the series of articles about the JSP Expression Language. In this article we will learn how to work with JSP EL functions. The JSP expression language allows you to define a function that can be invoked in an expression. Functions are defined using the same mechanisms as custom tags. The EL Function are public static methods in java classes, which are mapped in the TLD. As far as the Tag Library is concerned, each Tag library might include zero or more static functions that are listed in the TLD. To use a function in a JSP page, you use a taglib directive to import the tag library containing the function. Then you preface the function invocation with the prefix declared in the directive.

For example, the date example page index.jsp imports the /functions library and invokes the function equals in an expression:

<%@ taglib prefix="f" uri="/functions"%>
<c:whentest="${f:equals(selectedLocaleString,localeString)}" >

At first glance, functions seem similar to method expressions, but they are different in the following ways:

Functions refer to static methods that return a value. Method expressions refer to non-static, arbitrary public methods on objects.

Functions are identified statically at translation time, whereas methods are identified dynamically at runtime.

Function parameters and invocations are specified as part of an EL expression. A method expression only identifies a particular method. The invocation of that method is not specified by the EL expression.

Defining Functions:

To define a function, program it as a public static method in a public class. The pkg1.My in the date example defines a function that tests the equality of two Strings as follows:

package pkg1;
public class My
{
public static boolean equals( String l1, String l2 )
{
return l1.equals(l2);
}
}

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>pkg1.My</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.

Types Of EL Functions:

There are many types of EL functions that we are used, some important are as follow:

Name Description
Simple The majority of functions in the Stream Base expression language are simple functions, which operate on a single tuple field at a time. example: abs, cos, inet-aton() etc.
Aggregate Aggregate functions are used on sets of data to return a single result. Aggregate functions evaluate columns of data from windows or tables. example: aggregatelist(), concat() etc.
Timestamp which return an absolute timestamp or a string converted from an absolute timestamp. example: date, epoch(),now() etc.

Name schema

 

The generated function's name is the name of the named schema, and it takes one or more comma-separated expressions as value arguments. example: point(x,y) etc.

Using EL Function:

To illustrate the use of functions, I use a simple example to add two numbers. First we write the
Java code for the method to add two numbers. Code Sample 3 shows a static method that accepts
two Strings, parses them to integers, and returns their sum.

Compute.java:


package jsp2.examples.el;

import java.util.*;

public class Compute {
public static int add(String x, String y)
{
int a = 0;
int b = 0;
try
{
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {}
return a + b;
}
}

Once this is successfully compiled using javac,the next step is to map the function's signature in the tag library. Now, we can write the JSP page that uses the function. The user enters two numbers and when the "Add Numbers" button is clicked, the function is called to add the numbers. The result is displayed on the same page.

math.jsp:

<%@ taglib prefix="my" uri="http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"%>

<HEAD>
<TITLE>Functions</TITLE>
</HEAD>

<BODY>

<H3>Add Numbers</H3>
<P>
<FORM action="math.jsp" method="GET">
X = <input type="text" name="x" value="${param["x"]}">
<BR>
Y = <input type="text" name="y" value="${param["y"]}">
<input type="submit" value="Add Numbers">
</FORM>

<P>
The sum is: ${my:add(param["x"],param["y"])}

</BODY>
</HTML>

OUTPUT:

math.jsp:

Untitled-1.gif
The another topic link related to EL are given bellow:
  1. JSP Expression Language:http://www.c-sharpcorner.com/UploadFile/0d4935/jsp-expression-language/
  2. Working with implicit EL object: http://www.c-sharpcorner.com/UploadFile/0d4935/working-with-implicit-el-object/


Similar Articles