parsing error while including taglib

567 views Asked by At

I'm trying to use Apache Commons Lang 3 in my JSF 2 application, and I followed BalusC's example

But when I included this line in my .xhtml page :

<%@taglib prefix="f" uri="/WEB-INF/functions.tld" %>

I have an error while parsing the page code.

How can I fix the problem?

1

There are 1 answers

0
BalusC On BEST ANSWER

The answer was targeted on a question whose asker is known to use JSF 1.x on JSP. The syntax which you've there is specific to JSP, the legacy predecesor of Facelets which is deprecated since JSF 2.0.

Get rid of the functions.tld file altogether. The proper JSF 2.x Facelets way of declaring a custom function based on an existing static method is as follows:

  1. First create a /WEB-INF/functions.taglib.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <facelet-taglib 
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
        version="2.0">
        <namespace>http://example.com/functions</namespace>
    
        <function>
            <function-name>escapeJavaScript</function-name>
            <function-class>org.apache.commons.lang.StringEscapeUtils</function-class>
            <function-signature>java.lang.String escapeJavaScript(java.lang.String)</function-signature>
        </function>
    </facelet-taglib>
    
  2. Then register it in /WEB-INF/web.xml:

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/functions.taglib.xml</param-value>
    </context-param>
    

    (that step is unnecessary when it is placed in /META-INF of a JAR file which is in turn placed in /WEB-INF/lib)

  3. Finally declare and use it as follows:

    <html ... xmlns:func="http://example.com/functions">
    ...
    <h:outputScript>var foo = '#{func:escapeJavaScript(bean.foo)}';</h:outputScript>
    

Note that I've updated the answer you found accordingly. Also note that this function is already provided out the box as #{of:escapeJS(bean.foo)} by JSF utility library OmniFaces, in case you're using it.