Given a scenario, write EL code that uses a function; write code for an EL function; and configure the EL function in a tag library descriptor.

The EL has qualified functions, reusing the notion of qualification from XML namespaces (and attributes), XSL functions, and JSP custom actions. Functions are mapped to public static methods in Java classes. In JSP 2.0 the map is specified in the TLD. The full syntax:

ns:func(a1, a2, ..., an)

As with the rest of EL, this element can appear in attributes and directly in template text.

The prefix ns must match the prefix of a tag library that contains a function whose name and signature matches the function being invoked (func), or a translation error must occur. If the prefix is omitted, the tag library associated with the default namespace is used (this is only possible in JSP documents).

In the following standard syntax example, func1 is associated with some-taglib:

<%@ taglib prefix="some" uri="" %>



In the following JSP document example, both func2 and func3 are associated with default-taglib:

<some:tag xmlns=""
		<some:other value="${func2(true)}">



The Tag Library Descriptor (TLD) associated with a tag library lists the functions.

Each such function is given a name (as seen in the EL), and a static method in a specific class that will implement the function. The class specified in the TLD must be a public class, and must be specified using a fully-qualified class name (INCLUDING PACKAGES). The specified method must be a public static method in the specified class, and must be specified using a fully-qualified return type followed by the method name, followed by the fully-qualified argument types in parenthesis, separated by COMMAS. Failure to satisfy these requirements shall result in a translation-time error.

A tag library can have only one function element in the same tag library with the same value for their name element. If two functions have the same name, a translation-time error shall be generated.

The expression language allows you to define functions that can be invoked in an expression. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD). Write class with STATIC function:

package com.example; 

public class MyELFunctions {
	public static String concat(String str1, String str2) {
		return str1 + str2;
In order to use concat method we have to add a function element to our tag library descriptor (TLD). You'll have to create a TLD file if it doesn't already exist. A tag library descriptor defines and configures tags in a tag library. Here is /WEB-INF/example-taglib.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="" 
	web-jsptaglibrary_2_0.xsd" version="2.0">
		<description>Concatenates two strings</description>
			java.lang.String concat(java.lang.String, java.lang.String)

Add a taglib element to the deployment descriptor ( /WEB-INF/web.xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="" 
	xsi:schemaLocation=" web-app_2_4.xsd" 

Notice the taglib-location specifies the location of the TLD. The taglib-uri is, for the most part, an arbitrary name given to the tag library. The name you give it can't conflict with other tag libraries in your deployment descriptor. Adding the taglib element to the deployment descriptor is actually OPTIONAL. You could instead reference the TLD directly in the taglib directive on JSP:

<%@ taglib prefix="my" uri="/WEB-INF/example-taglib.tld" %>

This is NOT recommended because it reduces flexibility if you ever choose to rename or move the TLD. The uri would have to be changed in every JSP that used it.

The new JSP looks like the following:

<%@ taglib prefix="my" uri="" %>
	<head><title>EL Function example</title></head>
		str1 is : ${param["str1"]} <br>
		str2 is : ${param["str2"]} <br>
		concatenated : ${my:concat(param["str1"], param["str2"])} 

The prefix 'my' given in the taglib directive is whatever you choose to distinguish it from tags and functions in other tag libraries used in the JSP.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide