Show TOC

Developing EL FunctionsLocate this document in the navigation structure

Context

The EL enables you define your own functions to use in JSPs in your Web applications. You develop EL functions similarly to custom tags.

Procedure


  1. EL functions are mapped to public static methods in Java classes. To create an EL function, you create a Java file and implement your methods.

  2. The implementation of the function must be available in the application classpath, thus you package the file under WEB-INF/classes. You can also bundle several classes in a JAR file. In this case you package the JAR under WEB-INF/lib.

  3. To use the function in a JSP, you must first create a TLD and describe the function, for example myTLD.tld.

    Caution

    Starting with JSP 2.1, the TLDs you create must exactly follow the order of the elements as described in the TLD schema in the JSP 2.1 specification. The mandatory elements in a TLD are the taglib and short-name elements. Depending on the component you wish to use in your Web application, you also use a tag, tag-file, or function element respectively. We recommend that you also include the uri element. The order of elements defined in the schema is the following:

    description

    display-name

    icon

    tlib-version

    short-name

    uri

    validator

    listener

    tag

    tag-file

    function

    Sample Code
    <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd version="2.1"> 
    <short-name>my</short-name>
    <uri>http://com.sap.docs/FunctionsTaglib</uri>
    <function>
    <name>myFunction</name>
    <function-class>com.sap.demo.examples.myFunction</function-class>
    <function-signature>java.lang.String myFunction(java.lang.String)</function-signature>
    </function>
    </taglib>
    
    
                            
  4. You import the TLD in the JSP page using the URI attribute of the taglib directive.

    Sample Code
    <%@taglib prefix="my" uri="http://com.sap.docs/FunctionsTaglib" %>
                            
  5. You use the function in your JSP using the prefix defined in the taglib directive.

    Sample Code

    You print a greeting to the current user:

    ${my:myFunction(user)}