Home > java > JSP tag files – Simple typesafe markup reuse

JSP tag files – Simple typesafe markup reuse

Disclaimer: This is old information. But I still often talk to Java or JSP developers who were not aware of this feature, even though it was introduced in JSP 2.x, many years ago.

Another disclaimer: When I say “typesafe” I mean that tag files support the declaration of parameter types and some advanced IDEs like Intellij will actually use that information at source edit time to help the developer avoid runtime errors. If you want to get more reliable type safety independent of the IDE, you might want to consider a JSP pre-compilation approach like this one.

Ok, so now to the actual thing I wanted to write about:

If you have repetitive markup in your JSP pages you have several options for reuse, including:

  1. The <% @include %> directive for static file inclusion
  2. <jsp:include> or <c:import> tags for runtime inclusion
  3. Complicated “classic” JSP custom tags with tag handler Java code

All of these have limitations or problems: Static includes do not support any parametrization, jsp:include and c:import tags only support String parameters (via nested jsp:param tags) and custom tags with tag handlers and tab library descriptors require writing and compiling Java code that generates markup which is a bad practice and complicated.

JSP tag files, introduced in JSP 2.0 (JEE 1.4) solve all these problems and allow simple, straightforward and compact markup reuse that supports parameters of any Java type. The reference documentation is in the JEE 1.4 tutorial.

For example, put these lines into WEB-INF/tags/email.tag:

<%@ attribute name="p" required="true" type="net.doepner.Person" %>

<span title="Send email to ${p.name} &lt;${p.email}&gt;">
    <a href="mailto:${p.email}">${p.name}</a>
</span>

Then you can use it in any of your JSP pages as a tag like this, assuming the page has access to a “company” bean that has a getBoss() method:

<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>

<x:email p="${company.boss}" />

The good part about declaring the attribute type in your tag file is that IDEs like IntelliJ can provide all the nice things like code completion, refactoring support, javadoc display, etc for the EL expressions, like p.email or p.name in the example.

If the Java type of the “company” bean is known to the IDE, then it will even show an error in case the return type of getBoss() was not a subtype of Person.

Other nice things about the tag file approach:

  • No tag library descriptor (tld) file required
  • Tag attributes have local scope (not polluting the session, request or page scope)

Packaged tag files

A very cool thing about tag files is that they can be packaged in jars and reused in other web applications. This page in the J2EE 1.4 tutorial explains the details.

Advertisements
Categories: java
  1. Kacper86
    August 20, 2013 at 10:40

    Nice article! Do you know if it supported in Eclipse or STS? Because I couldn’t get it to work.

    • August 20, 2013 at 14:39

      Yes, this is a JSP feature, not IDE dependent.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: