Tuesday, March 13, 2012

Creating Custom functions in JSP using JSTL

1. Create a tld file inside WEB-INF folder having declaration of the custom function as below
    <?xml version="1.0" encoding="UTF-8" ?>
      <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-jsptaglibrary_2_0.xsd"
          version="2.0">
   
          <description>JSTL 1.1 functions library</description>
          <display-name>JSTL functions</display-name>
          <tlib-version>1.1</tlib-version>
          <short-name>fn</short-name>
          <uri>http://java.sun.com/jsp/jstl/functions</uri>
        <function>
              <name>functionName</name>
              <function-class>anshul.project.vaidya.test.Anshul</function-class>
              <function-signature>
                java.util.ArrayList&lt;java.util.ArrayList&gt; functionName( java.lang.String )
          </function-signature>
        </function>
    </taglib>

2.     Configure this tld file in web.xml as
    <jsp-config>
          <taglib>
                <taglib-uri>$customURI</taglib-uri>
                <taglib-location>$Absolute path of tld file inside WEB-INF starting with / </taglib-location>
          </taglib>
      </jsp-config>

3.     Now to use the function in a jsp page add the taglib directive there as

    <%@ taglib uri="$customURI" prefix="$customPrefix"%> 
     
    Now you can use the function anywhere in the page like:-

    ${customPrefix:functionName(parameter)}

No comments:

Post a Comment

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...