This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
    Bookmark Topic Watch Topic
  • Mark post as helpful
  • send pies
  • Report post to moderator
Under JSP 2.0 you can define functions that can be referenced from within EL expressions. For full details, please see section JSP.2.6 in the JSP 2.0 Specification:

Let's say that you want to use some static functions such as those defined by the Jakata Commons StringEscapeUtils class ( from within your EL expressions. In particular, let's pick the escapeJavaScript method whose signature is:

In order to define an EL function, a <function> element must be added to an existing or new TLD file that is in the scope of your application and declared within the referencing JSP page. For our example, if we wanted to name the EL function the same name as the static method (though they do not need to be the same), we could add:

to a TLD file in our web application. (Of course, this class must be available in the classpath of the web app).

Note that the class name of the enclosing class must be fully qualified, and note that the class names in the signature must also be fully qualified.

On a JSP page where you wish to reference your new EL function, you would declare the taglib as any other. For example:

and then you could use the function in any EL expression on that page. Be sure that the uri value matches the URI defined within your TLD file. For example:

Let's say that we have a scoped variable named someText that contains text that we want to emit into the context of a JavaS'cript string. In order to make sure that there will be no quoting problems, we want to call our escape function on the string so that any embedded quotes will be escaped and will not interfere with the syntax of the JavaS'cript notation on the page.


Cool stuff, no?

    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!