Article
The JSP Files - Parts 1 to 8: Tagged and Bagged
Page: 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 32 33 34 35 36 37 38 39 Next
Meeting Popeye
At this point, you can begin using the new tag library in your JSP pages. First, declare it with the "taglib" directive - this directive must appear before any custom tags in the page.
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
The URI is the unique identifier for the tag library, and must match the URI specified in "web.xml", while the prefix appears in every call to a custom tag, and is used to distinguish between tags from different libraries in the same page.
Once the library has been declared, you can begin using custom tags in your JSP scripts. Consider the following example, which uses a custom tag from the DATETIME library to calculate the number of seconds elapsed since January 1 1970.
<html>
<head>
</head>
<body>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
The number of milliseconds since January 1, 1970 is <popeye:
currenttime/>
</body>
</html>
And the output is:
The number of milliseconds since January 1, 1970 is 987165837280
What if you simply want the current date and time? By combining the <currenttime> tag with the <format> tag, the DATETIME library makes it a snap!
<html>
<head>
</head>
<body>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
The current date and time is
<popeye:format pattern="hh:mm EEE MMMM dd yyyy"> <popeye:
currenttime/> </popeye:format>
</body>
</html>
In case you're wondering, the EEEs and MMMs you see there are formatting codes, used to define the format in which the date and time is to be printed. Here's the output:
The current date and time is 06:22 Fri April 13 2001
The example above also illustrates how some tags can be nested within one another - this can make for powerful combinations, and is one of the clever things about this architecture.
How about generating a list of days or months? The DATETIME library's got you covered with its <weekdays> and <months> tags...
<html>
<head>
</head>
<body>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
<form>
Select a day
<select name="weekday">
<popeye:weekdays id="day">
<option value="<jsp:getProperty name="day" property="dayOfWeek"/>
"> <jsp:getProperty name="day" property="weekday"/> </popeye:weekdays>
</select> </form>
</body>
</html>
The DATETIME library comes with a whole bunch of other useful tags too - take a look at the documentation to see the various other features available.
Copyright Melonfire, 2000. All rights reserved.