Wednesday 6 May 2015

How to create a custom Function for JSTL.

JSTL Custom Function : In this article I am going to teach you how to create custom JSTL

function. We had used many JSTL functions which are already in the lib.But if you want to

create a custom function for JSTL you need to follow some easy steps.

Step 1: Create tag library descriptor file which defines the configuration of custom function,

like it's class and the function which is open to use. Create the .tld file inside the 'WEB-

INF/tld/ ' directory .

/WEB-INF/tld/customTagLibrary.tld

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
        <display-name>Custom EL Functions</display-name>  
        <tlib-version>1.0</tlib-version>
        <short-name>cg</short-name>
        <uri>/WEB-INF/tld/customTagLibrary</uri>
        <function>
            <name>getSum</name>
            <function-class>com.evon.example.CustomELFunction</function-class>
            <function-signature>
                String getSum(int, int)
            </function-signature>
        </function>
    </taglib>

Step 2 : Create an entry in web.xml for this tld file information:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-

app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <jsp-config>
          <taglib>
              <taglib-uri>/WEB-INF/tld/customTagLibrary</taglib-uri>
              <taglib-location>/WEB-INF/tld/customTagLibrary.tld</taglib-location>
          </taglib>
      </jsp-config>
    </web-app>

Step 3 : Create a class file which should match as described in function-class tag in tld

file. Note: Method signature must include static identifier.

CustomELFunction.java

    package com.evon.example;
    public class CustomELFunction {
        public static String getSum(int x, int y){
            int sum = x+y;
            return sum;
        }
    }

Step 4 : Finally we need to create a jsp file, where we’re going to call custom function using

jstl. First we need to define the object of that taglib using jsp directives <%@ taglib %>.

Following the jsp which calling a custom JSTL method using Expression Language.

copytext

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-

8859-1"%>
    <%@ taglib uri="/WEB-INF/tld/customTagLibrary" prefix="cg" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Custom Function </title>
    </head>
    <body>
    ${cg:getSum(25+30) }
    </body>
    </html>

For such more Blogs kindly visit to http://findnerd.com/NerdDigest

No comments:

Post a Comment