home.


Tagged: jsp-el


JSP: Creating a custom EL function

Note I’m creating this custom EL tag in Tomcat 7.

First make a class with a static method like so:

    package your.package;

    public class Functions {

       public static String hello(String name) {
         return "Hiya, " + name + ".";
       }

    }

Then make a file called mytaglib.tld in WEB-INF/tags/:

    <?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/javaee/web-jsptaglibrary_2_1.xsd" 
      version="2.1"> 

      <tlib-version>1.0</tlib-version>
      <uri>http://www.your.url/tablib_name</uri>

      <function>
          <name>hello</name>
          <function-class>your.package.Functions</function-class>
          <function-signature>java.lang.String hello(java.lang.String)</function-signature>
      </function>  

    </taglib> 

Note the function part, which specified the name of the new EL function, what class it resides in and its function signature.

The uri would be used if we were accessing this directly, but instead we’ll be accessing the url from the web.xml below:

    <web-app 
        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_4.xsd"
        version="2.4">

        ...

       <jsp-config>
       <taglib>
         <taglib-uri>
           http://some.thing/mine
         </taglib-uri>
         <taglib-location>
           /WEB-INF/tags/mytaglib.tld
         </taglib-location>
       </taglib> 
       </jsp-config>

       ...

     </web-app>

Note we’re pointing to the mytaglib.tld file just created. And taglib-uri is how we’ll refer to it in the JSP:

    <%@ taglib uri="http://some.thing/mine" prefix="a" %> 

    ${a:hello("Aaron")}
jsp jsp-el el-custom-function

JSP: Setup JSTL and EL in Tomcat 7

If you want to use JSTL (and you do) instead of scriptlets you first have to include it in your war file or your tomcat lib file.

I imported it into my project using Gradle. You should see the jar file in the created WAR file once you’re using it.

Now you can use things like:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
    <c:forEach var="p" items="${applicationScope['products'].values().iterator()}">

       ${p.name} | ${p.getPrice()} || <a href="Cart?add=true&id=${p.getId()}">add</a>
       <br />

    </c:forEach>

Note you don’t need the scriptlet %@ page import.. directives anymore. You do need thee taglib directive to point to the jstl core library.

Note we’re using the JSP EL, expression language, syntax to call method and attribute (p.name is the same as p.getName()). EL comes with Tomcat 7 - no additional libraries are required.

As with scriplets there are implicit objects in EL, applicationScope, in this example. ’applicationScope[‘products’] is the same as applicationScope.getAttribute(‘products’).

jsp jstl jstl-foreach tomcat jsp-el

Page 1 of 1