JSP Tag Libraries






JSP Tag Libraries

We have already used several tag libraries in this book, primarily in Chapter 7, "The Spring Web MVC Framework," where we discussed the Spring Web MVC Framework. These tag libraries include the spring:bind library, JSTL, and others. One of my favorite tag libraries is Displaytag, so let's look at that next.

Displaytag

Displaytag, according to its website (displaytag.sourceforge.net), "is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use."

I've used this tag library before and love its simplicity and robustness. For example, in fewer than 20 lines of JSP code, we could generate a sophisticated HTML table using data from a database via JDBC. We will use it to convert the HTML table on our Timesheet List screen, which we originally generated using a combination of JSTL's c:forEach and c:out tags. The key benefit for our sample application is the capability to sort the list and also export it to a PDF file, CSV file, or other formats, if needed.

The following code excerpt from our refactored timesheetlist.jsp file (found under timex2/) shows how the Displaytag library works:

<%@ taglib prefix="display" uri="http://displaytag.sf.net/el" %>
<display:table name="timesheets" id="timesheet" defaultsort="1"
        requestURI="timesheetlist.htm"
        cellpadding="5" cellspacing="0"
        export="false" class="tableborder">
<display:column property="department.name" sortable="true"
            title="Department"/>
</display:column>

This tag library can be downloaded from displaytag.sourceforge.net along with documentation on additional features of Displaytag. As explained on the web, this has several dependencies. Follow the instructions on the website to install it for our sample application.

Writing Custom Tag Libraries

Although many tag libraries are available on the web, at times you will need to write a custom tag library that is specific to your project. In the past, I have had to write a custom tag library to hide certain buttons on a screen based on the authorization level of the user.

Our PayPeriodCheckTag.java file shows a complete and functional custom tag library used for Time Expression. This tag library is a simple demonstration of what tag libraries can be used for. This particular tag library checks the current date against the period starting and ending dates. If the current date is outside this start and end date range, the Save button is not displayed on the screen because the user is only allowed to update the current pay period's timesheet.

The following snippet from PayPeriodCheckTag.java shows how we extend the javax.servlet.jsp.tagext. TagSupport class and provide a custom tag library.

public class PayPeriodCheckTag
    extends TagSupport
{
  private Date checkDate;
  public int doStartTag() throws JspException
  {
    boolean includeText = (DateUtil.isInCurrentPayPeriod(checkDate));
    if (includeText)
      return TagSupport. EVAL_BODY_INCLUDE;

    return TagSupport. SKIP_BODY;
  }

The following code shows an excerpt from the supporting tag library descriptor (TLD) file, timex.tld.

<shortname>timex</shortname>
<tag>
  <name>periodcheck</name>
  <tagclass>com.visualpatterns.timex.util. PayPeriodCheckTag</tagclass>
  <attribute>
    <name>checkDate</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>

This code excerpt from our enterhours.jsp file shows how the timex:periodcheck tag is used to either show or hide the Save button.

<%@ taglib prefix="timex" uri="/WEB-INF/timex.tld"%>
<timex:periodcheck checkDate="${command.periodEndingDate}">
  <input name="save" type="submit" value="Save">
</timex:periodcheck>



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows