EL Functions
Introduction:
1. The main objective of Expression Language (EL) is to separate java code from the Jsp.
2. If we have any business functionality we can separate it into a java class and we can invoke that functionality through Expression Language (EL) syntax.
3. 3. The page designer has to know only function name and tld file uri to get its functionality. Hence EL is almost treated as alternative to custom tags.
Designing Expression Language (EL) function application contains the following steps.
1. Writing a java classes with required functionality.
2. Writing tld file to map java class to the Jsp.
3. Write a taglib directive to make business functionality available to Jsp.
4. Write Expression Language (EL) function call.
1. Writing a Java Class:
Any java class can simply acts as a repository for EL functions.
The only requirements of a method that acts as EL function it should be declared as public & static.
Method can take parameters also.
No restrictions on return type void return types also allow.
2.Writing tld file: (tag library descriptor)
For Expression Language functions tld file provides mapping between Jsp [where functionality is required] and java class [where functionality is avialble].tld file is an xml file.
We can configure Expression Language (EL) function by using function tag in this tld. This tag defines the following four child tags.
1. <description>
2. <name>
a. By means of this name only we can call EL functionality in the Jsp.
3. <function-class>
4. It defines fully qualified name of java class name where Expression Language (EL) function is available.
5. <function-signature>
Signature of the method .
Example:
<function> <name>upper</name> <function-class>StrMethods</function-class> <function-signature>java.lang.String upper(java.lang.String)</function-signature> </function> |
3.Writing tagLib Directive:
This is to make EL functionality available to the Jsp.
<%@ taglib prefix="myString" uri="StringOperations" %>
4.Invoking EL Functions:
${myString:upper(param.name)}
EL Function Flow:
1. Where Jsp engine encounters EL functions call with prefix and EL function name then it checks for corresponding taglib directive with matched prefix.
2. From taglib directive Jsp engine identifies uri and checks for tld file with matched uri.
3. From the tld file Jsp engine checks for corresponding class and required method.
4. Jsp engine executes that method and return its result to the Jsp.
Example: TestELServlet.java
Package com.myjavahub;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
public class TestELServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request,response); } } |
result.jsp
<%@ taglib prefix="myString" uri="StringOperations" %> <html> <body> <h1 align="CENTER">Result</h1> ${myString:upper(param.name)} </body></html> |
index.html
<html> <body> <h1 align="CENTER">Test Expression Language</h1><br><br> <form method="GET" action="MyTestEL.do"> Name: <input type="text" name="name"/><br> <input type="submit" name="command" value="submit"/> </form></body></html> |
My.tld
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
<tlib-version>1.2</tlib-version> <uri>StringOperations</uri> <function> <name>upper</name> <function-class>StrMethods</function-class> <function-signature>java.lang.String upper(java.lang.String)</function-signature> </function>
</taglib> |
my method java file is:
public class StrMethods { public static String upper(String s) { return s.toUpperCase(); } } |