Notes of a (Java) developer

JSTL (and EL)

Software developer Programmer

Custom EL functions

The first official documentation available on the topic is The Java EE 5 tutorial. The JSTL specification allows to define custom functions. This is done easily with simple static methods specified in a Tag Library Definition (tld) file. For example, a file located at /WEB-INF/tags/my-functions.tld:

<?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/web-jsptaglibrary_2_0.xsd"
	version="2.0">
    <tlib-version>2.0</tlib-version>
    <uri>/WEB-INF/tags/my-functions.tld</uri>
    <function>
    	<name>myMethod</name>
        <function-class>my.package.MyFunctions</function-class>
        <function-signature>java.lang.String myMethodSignature( java.lang.String )</function-signature>
    </function>
</taglib>

Can be loaded by the JSP that way (the URI can be improved);

<%@ taglib prefix="mine" uri="/WEB-INF/tags/my-functions.tld" %&gt>

Then the static method is called with a simple expression using the specified prefix:

${mine:myMethod('my string value')}

"fn" (functions)

There is a problem in testing the content of an HTTP request URL: the HttpServletRequest method returns a StringBuffer. The fn:contains(.., ..) function accepts only strings. Using the getRequestURL() result as an argument works fine. But this is not static-type conform. Here the Eclipse IDE Web page validation complains.

In order to code in a static-type way, the solution is to set the StringBuffer in a variable to transform it into a string. Then the function can be used properly without any validation problem:

<c:set var="requestUrl" value="${pageContext.request.requestURL}" />
<c:if test="${fn:contains(requestUrl, '...')}">
</c:if>

"fmt" (messages)

Using a message tag (fmt) with param children, I had to escape quotes unless the message would not include the parameters.

Java Web