Calendar JSP Tag Home Page

Boyd Waters bwaters@nrao.edu

News

Sat Jan 13 22:46:27 2001
  • NAVIGATION ENABLED! You can now define specific URLs for dates, and/or set a default URL that's calculated from the date! See navigation
 
Thu Dec 21 22:27:11 2000
  • Added locale attribute! You can now specify the desired locale as a tag attribute or as a pageContext attribute named "locale"! See localization
  • Verified XHTML output on Lynx/Solaris, IE 5.0/WinNT-4.0, Moz0.6/Linux, Netscape 4.75/Linux
 
Thu Dec 21 00:52:55 2000
  • display parameter added!
  • today parameter added!
  • Layout bugfixes!
 
Tue Dec 19 21:05:16 2000
Initial Posting

About the CalendarTag JSP Tag

The CalendarTag is a Java JSP Taglib extension intended to be submitted to the Jakarta Apache Taglib Project which emits a calendar display for the current month in clean, CSS-formatted XHTML, suitable for display on a wide variety of web browsers, from Mozilla to WAP phones.

The calendar HTML on this page was generated by this tag (although the page that you're looking at is a static web page, not dynamic Java JSP).

The calendar can be used as a navigation aid for time-based data; see Navigation.

You may specify a locale in order to display the day and month names in various languages. However, this is a Gregorian calendar; different calendar systems such as Hebrew, Islamic, and Chinese calendars (to name a few) are not handled at this time. I am quite illiterate when it comes to alternate calendars, and therefore have no plan to implement alternate calendars. But if someone wants to help...

As a quick n' dirty example, this line of code in my HTML page: <dt:calendar /> results in this output HTML (when run on December 19, 2000, on a machine set up for the United States locale as default):

<p>
<pre class="calendar">
<span class="calMonth">December 2000</span>
<span class="calDays"> Sun Mon Tue Wed Thu Fri Sat</span>
<span class="calPrev"> 26  27  28  29  30 </span>  1   2 
  3   4   5   6   7   8   9 
 10  11  12  13  14  15  16 
 17  18 <span class="calNow"> 19 </span> 20  21  22  23 
 24  25  26  27  28  29  30 
 31 <span class="calNext">  1   2   3   4   5   6 </span>
</pre>
</p>

Getting the CalendarTag

[ Sample Warfile ]   [ Java source code ]

As I write this, (19 Dec 2000), the CalendarTag is two hours old, and is therefore not yet incorporated into the Jakarta Taglibs distribution. In the interest of getting the code out there, you can obtain the Java source code and the Sample Warfile from this web site. (NOTE: This web server does not set the document type correctly for the warfile. Tell your browser to save the link/target to disk!)

When/if this is approved for incorporation into the Taglibs project, it will probably be a part of the datetime taglib, and you will be able to get the CalendarTag by getting the taglib binary distribution or the taglib source distribution.

Installation and Usage

Until this tag is incorporated into the Jakarta Taglib distribution, you can try these steps to get the taglib working.

I suggest you get the Sample Warfile and deploy it in your JSP container in the manner specifed for war files. I've tested this with Tomcat-4.0 -- for this JSP server, you can simply:

  1. Place the warfile in Tomcat's webapps directory
  2. Re-start Tomcat (or use the manager app if that's working these days)
  3. The sample calendarTag web app is in your serverroot/calendarTag -- so for my workstation, I open my web browser and type
    http://myservername:8080/calendarTag
    to examine the calendarTag sample app.

Other JSP containers might have different methods, I don't know. For those of you not using Tomcat, here's some info that may or may not help...

A "war" file (web application archive) is just a Java JAR file that contains deployment descriptors, class libraries, and html files for a JSP 2.2 application. Given that it's just a JAR file, it's also just a ZIP file, so if you really need to take it apart, you can unzip it and look at the deployment descriptor files and the sample JSP file. The Java source for the file isn't included in the warfile; you can see the Java source code here.

CalendarTag Usage Basics

Once you've got the sample webapp running, you know that you'll be able to add this tag to your own web applications:

  1. Copy the datetime.jar file in calendarTag/WEB-INF/lib to yourWebApp/WEB-INF/lib
  2. Copy the datetime.tld file in calendarTag/WEB-INF to yourWebApp/WEB-INF
  3. Add these lines to the file yourWebApp/WEB-INF/web.xml:
      <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/datetime-1.0</taglib-uri>
        <taglib-location>/WEB-INF/datetime.tld</taglib-location>
      </taglib>
  4. Add this line to your JSP files before the ocurrence of any calendarTag lines:
    <%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="dt" %>
  5. Place this JSP line in your JSP file anywhere you want a calendar for the current month to appear:
    <dt:calendar />

TODO: Discuss doing steps 4 and 5 with the new XML JSP file syntax

Parameters

At the moment, (13 Jan 2001) there are FIVE optional parameters that you can set:

CalendarTag Optional Parameters
display Specify the month, in "MM/yyyy" format, that you want displayed, e.g.:
<dt:calendar display="6/2000" />
will display a calendar for June of the year 2000.
today Specify the date, in "MM/dd/yyyy" format, that you would like indicated as today's date, e.g.
<dt:calendar display="6/2000" today="5/29/2000" />
will display a calendar for June of the year 2000, with May 29th highlighted as the current date.
locale Specify the language code and country code for the locale that you want, e.g.:
<dt:calendar locale="pt BR" />
will display a calendar for the Brazillan Portuguese locale. The locale attribute can be delimited by any of the space ' ', comma ',' , semi-colon ';', or dash '-' characters, so these are all valid, too (I think): "pt-BR" "pt;BR" "pt, BR".
Note that there is an optional third attribute which you can pass, which is a "locale variant" specifier, which might further refine a resource bundle depending on your Java environment. See the java.util.Locale documentation for more details.
<dt:calendar locale="pt BR Traditional_POSIX" />
Finally, note that the calendar tag will use the locale specifed in the pageContext using the findAttribute() call, which means that it will search for a Locale object named "locale" in page, request, session (if valid), and application scope(s) in order. So you can specify a Spanish (for example) display of the calendar tag by adding a pageContext directive in your JSP page:
<% pageContext.setAttribute( "locale", java.util.Locale.SPAIN ); %>
navigate The CalendarTag will form URLs for the days in the calendar based on the date if either of the following are true:
  1. The BODY of the tag contains date listings
  2. You "force" URLs to be generated by setting the navigate attribute. (I usually say navigate="true" but any string will do, except that setting it to "false" will explicitly disable URL generation.)
See navigation for more information.
urlFormat You may optionally set this attribute to a valid SimpleDateFormat pattern string which will be used to generate the date-based URLs. See navigation for more information.

Navigation Now Possible!

My whole motivation for this taglib was to facilitate navigation on my XSLT-based RDF weblog (old static mirror here (shameless plug)). "A HREF" links work inside of PRE text for the browsers that I've tested, and appear to be the XHTML standard, so it's a question of how to specify these links to the tag. Here are the options currently implemented:

Examples

Navigation Enabled, no Dates Specified

January 2001
 Sun Mon Tue Wed Thu Fri Sat
 31   1   2   3   4   5   6 
  7   8   9  10  11  12  13 
 14  15  16  17  18  19  20 
 21  22  23  24  25  26  27 
 28  29  30  31   1   2   3 

The HTML for this tag looks like this:

<dt:calendar navigate="true" />

No tag body, no URL format specified, but navigation is enabled. Note that by default, the URLs which are generated are relative URLs. You can change this behavior by specifying an URL format.


Navigation Enabled, no Dates Specified, With an URL Format

January 2001
 Sun Mon Tue Wed Thu Fri Sat
 31   1   2   3   4   5   6 
  7   8   9  10  11  12  13 
 14  15  16  17  18  19  20 
 21  22  23  24  25  26  27 
 28  29  30  31   1   2   3 

The HTML for this tag looks like this:

<dt:calendar navigate="true" urlFormat="'javascript:requestMethod(''processCalendar'','''MM/dd/yyyy''')'" />

Specific Dates and URLs

Here's a calendar with particular days specified for navigation in the tag body, and a URL format for the navigation given for any dates that do not have a URL specified in the tag body.

January 2001
 Sun Mon Tue Wed Thu Fri Sat
 31   1   2   3   4   5   6 
  7   8   9  10  11  12  13 
 14  15  16  17  18  19  20 
 21  22  23  24  25  26  27 
 28  29  30  31   1   2   3 

The HTML for this tag looks like this:

<dt:calendar
urlFormat="'file:///C:/Documents%20and%20Settings/Administrator/My%20Documents/files/jot/'yyyy/MM/dd">
01/04/2001    http://www.monkeymeat.com
01/13/2001
</dt:calendar>

See how the first date has a URL specified for it, but for the second tag, we form a URL by formatting the specified date with the urlFormat attribute via a java.text.SimpleDateFormat object.

Note that you do NOT have to set an URL format; the format is only used if you would like control over default URL generation. If you wish, you may specify each URL for each date in the BODY tag, and never need to use an URL format:

<dt:calendar>
01/04/2001    http://www.monkeymeat.com
01/13/2001    file:///C:/WINNT/System32/
01/31/2001    http://www.w3.org
</dt:calendar>

You can leave off specifying the URLs and the URL format, and you'll get the default relative URLs for just the days that are mentioned in the BODY:

<dt:calendar>
01/04/2001
01/13/2001
01/31/2001
</dt:calendar>

Finally, note that if you specify dates that are not in the current month, you'll have to specify the month via the display property:

<dt:calendar display="06/2002">
01/13/2002
06/09/2002
06/14/2002
</dt:calendar>

The first date specified in this example occurs before June 2002, so it does not affect the display of that date; it is ignored.

I think that the display date should be inferred from the dates that occur in the tag body; if all (or most of) the requested dates in the tag body are in the same month, then that month should be the one that's displayed. Hmm. Well, for now, that's NOT how it works; you have to specify a calendar month if you want to display a month different from the current date.


Navigation Attribute Summary

To wrap up, here are the two navigation attributes again:

1. A SimpleDateFormat pattern string to produce URLs

<dt:calendar urlFormat="'javascript:requestMethod(''processCalendar'','''MM/dd/yyyy''')'" />

would result in markup like

<a href="javascript:requestMethod('processCalendar', '12/25/2000')">25</a>

This is totally easy from a programming standpoint, but it forces an awful mess upon you with respect to single-quote madness: a single single-quote is used to delimit literal text to appear in the URL -- that is, text not used to interpret the date formatting -- and double single-quotes are used to output a literal single-quote.

Well, if you're a JavaScript programmer, you're used to single-quote fun...

Another benefit of this approach is that the SimpleDateFormat strings are well-documented in the Java API documentation.

2. Mapping URLs to days - Simple Properties

The BODY of the tag can contain text that is compatible with the java.util.Properties serialization scheme:

<dt:calendar>
12/15/2000    http://www.monkeymeat.com
12/18/2000    http://www.foobar.com
12/31/2000    javascript:requestMethod( 'processCalendar', '12/31/2000' )
</dt:calendar>

Note that this method of mapping could be combined with the urlPattern attribute to give you formatted links for just the days that have entries:

<dt:calendar urlFormat="'http://www.grover.com/calServlet?date='yyyy/MM/dd">
12/15/2000
12/18/2000
12/31/2000
</dt:calendar>

Calendar Formatting with CSS

The HTML output by the CalendarTag is simple and XHTML-1.0 compliant. I rely upon CSS for my formatting. The output HTML is marked up with class attributes which permit CSS-compliant browsers to modify the appearance of the calendar elements, while minimizing the visual impact for non-conformant browsers.

In particular, this markup should look OK under text-only browsers such as Lynx or WAP phones.

In order to add formatting to your calendar, you need a CSS stylesheet. Here is the stylesheet that's used by this page:

<style type="text/css">
.calendar { background: #eeeeee;     font: 11pt monospace; }
.calMonth { background: transparent; font: bold 12pt sans-serif; }
.calDays  { background: #ccccff; }
.calPrev  { background: #cccccc; }
.calNow   { background: #ccffcc; }
.calNext  { background: #cccccc; }
.calHoliday{background: #ffcccc; }

.sidebar { background: #cccccc; float: right; border: 3px solid black; }
.timestamp { font-weight: bold; margin-top: 0.3em; }

.example { 
  background: white;
  margin-left: 2em; margin-right:2em;
  border: 1pt solid black;
}

body { font: 12pt/13pt sans-serif; background: #eeeeee; }
h1, h2, h3, h4 {  font-weight: bold }
h1   { font-size: 200% }
h2   { font-size: 150% }
h3   { font-size: 120% }
pre, code, dt { font-family: monospace }
strong{ font-weight: bold; }
</style>

Here is a description of the calendar CSS classes currently used:

calendar
Applies to the entire calendar markup. Add a style for this tag and it will apply to the whole calendar.
NOTE: Specify a monospaced font or the calendar items won't line up!
calMonth
The month name at the top of the calendar. This item can be a proportional font, unlike the other elements.
calDays
The day name header: e.g., "Sun Mon Tues..." You might want to specify a background color for this element to set off the day name header from the rest of the calendar
calPrev
calNext
These elements apply to the days which are displayed in the calendar from the previous and next month. I like to specify a grey or transparent background for these days to show that they are not in the current month.
calNow
This applies to the current day. I use a light green background. Note that the example HTML on this page is not dynamically generated, which is why December 19th, 2000 is highlighted as the current day. It will highlight the correct day when run as a taglib.

Bugs and Future Plans

Things I want to do, probably in order of importance to me:

Verify the HTML on other browsers

The CalendarTag HTML output should conform to the XHTML 1.0 - Strict specification. I have verified compliance by using the W3C XHTML 1.0 validation service.

I have tested the HTML generated by the CalendarTag with the following platforms and browsers:

Platforms and Browsers Tested
Windows 2000 Professional (Athlon PC) Internet Explorer 5.5,Mozilla M18, Mozilla 0.6, Mozilla 0.7, Netscape 4.76, XEmacs W3
Linux Mandrake 7.1 (Athlon PC) Mozilla M17, KDE Konqueror, Netscape 4.7
Solaris 2.6 (Sun SPARC Ultra1) Netscape 4.76, Lynx

Other test platforms are welcome, in particular Macintosh platforms (iCAB!) and Opera. Last summer I was working on getting Mozilla compiled for my Solaris box, but at this point it's easier to wait for our IS department to upgrade to Solaris 2.8.

I chose to go with monospaced, "pre" tag text instead of an HTML for two reasons: 1) the pre tag renders correctly on more common text-based browsers and 2) the HTML code is much smaller.

The Visual Studio.NET beta 1 script debugger complains about span anda hreftags occuring within the enclosingpre tag on my Windows 2000 computer, but that the HTML validates against the XHTML 1.0 Strict DTD. Perhaps the Visual Studio.NET beta is using a different definition of HTML.

To Do: XML-based URL Mappings

I am not certain exactly how XML would be used to specify the mapping between dates and URLs, but...

1. XML file specified by a parameter

<dt:calendar linkMap="cal.xml" />

2. XML in the Tag Body

Like the option above, but the linkMap XML is given explicitly in the body of the tag. I like this option because I can generate the map XML from an XSLT tag embedded in this tag's body, e.g.

<dt:calendar>
  <xsl:apply xsl="/xsl/rdf-to-linkmap.xsl">
    <xsl:include page="/rss/slashdot.rdf"/>
  </xsl:apply>
</dt:calendar>

To Do: Evaluate the HTML on aural browsers

-- is it accessible via a screen reader? This is important for people with vision difficulties, and also important to anyone developing an automated voice-jail system. I think that my "pre" text will read better on more HTML screen readers than a table represenation.

I have attempted to use Internet Explorer and the basic screen reader that ships with Windows 2000, but as I write this (January 2001), this combo doesn't pay attention to any of the features recommended by the W3C Accessibility commitee.

I will attempt to get XEmacs-W3 working with the Emacs reader; this seems to be a good platform for standards-compliant accessibility.