One of the keys of the Java 2 Enterprise Edition (J2EE) platform is that it enables programmers to have modular control over their applications. Importing classes, using JavaBeans and Enterprise JavaBeans (EJB) components, and using custom actions can separate the deeper programming logic from the site's layout. This allows everyone involved in Web development to focus on what they do best.
Programming modular components requires a thorough understanding of the Java programming language and object-oriented programming techniques. So although beans, classes, and custom actions undoubtedly help Web teams that have designers and Java programmers, smaller teams and individual Web developers may be unable to utilize these tools. Enter Macromedia's unique technology: JRun Server Tags (JST).
Throughout this article, we refer to these components as custom tags. However, tags and actions are often used interchangeably.
Programming custom JSP actions used to require a number of steps: creating the custom action itself by using the custom tag application programming interface (API), creating a Tag Library Descriptor (TLD) XML file, and sometimes creating a Tag Extra Information (TEI) class. As JSP compiles servlets, JST compiles custom tag classes, and creates TLDs on-the-fly. When these dynamically created classes are compiled, you can use them in any other servlet engine that supports JSP 1.1 custom tags.
In JRun, you can also create custom tags the old-fashioned way. This does, however, require an understanding of creating Java classes.
Creating JST Tags
You can create JST actions quickly and easily. Let's start with a simple example. Suppose that we have needed to get the current date and format it. This has taken only a couple lines of Java, but a custom tag could greatly simplify this task for us. Listing 1 contains the tag TimeDate, which we can use to return the current date and/or time, and specify the format in which it should be returned.
Listing 1TimeDate.jstCreating Custom Actions
<%-- Name: TimeDate.jst Author: Drew Falkman Description: Simple custom action Created: 7/29/01 Attributes: format = SimpleDateFormat formatting String default = "MM/dd/yyyy" --%> <%@page import="java.util.*,java.text.*" %> <%-- define attribute "format" and default value --%> <%@tagAttribute name="format" required="false" default="MM/dd/yyyy" %> <%-- creat date formatter and get current date --%> <% SimpleDateFormat SDF = new SimpleDateFormat(format); Date now = new Date(); %> <%-- output formatted date --%> <%= SDF.format(now) %>
First, notice the comment. When creating custom tags, it is always a good idea to note the attributes that can be used, along with their defaults and whether they are required.
The next important piece to notice is the tagAttribute directive. This directive is used in JST templates to define attributes, whether they are required, their default values, and more. The name attribute specifies the name format. It is not required, so that attribute is set to false. Finally, the default value is set to MM/dd/yyyy.
Next, the scripting begins. The rest of this page looks like any other JSP page: Create the SimpleDateFormat object, create the new Date object, and output it.
Note that the String argument passed to the SimpleDateFormat class is the variable format. This passes the value of the attribute (if it was specified) or the default value of MM/dd/yyyy, as was specified in the tagAttribute directive.
The next step is to call on the new tag. Make sure that the TimeDate.jst file is saved in the /22 directory in the ows application. Then create the Caller1.jsp template, as shown in Listing 2, in the same folder. The results should be what you see in Figure 1.
Listing 2Caller1.jspCalling the TimeDate.jst Template
<%-- Name: Caller1.jsp Author: Drew Falkman Description: Calling TimeDate JST Created: 7/29/01 --%> <%@taglib uri="/22/" prefix="jwack" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Using Custom Actions</title> </head> <body> <%-- call TimeDate.jst custom tag --%> <jwack:TimeDate format="MMMM d, yyyy" /> </body> </html>
Figure 1 The custom action <jwack:TimeDate> returns the current date in whatever format is passed in the format attribute.
You can see how this can make your life easier and allow you to reuse JSP code better: You make a custom tag once and then use that tag as many times as you want in the future.
In the template in Listing 2, the taglib directive specifies the folder, /22/, and the prefix jwack (short for JRun Web Application Construction Kit). Then, later in the JSP template, the custom tag is called. What is especially nice about this example is that you can format the date and time with as much flexibility as you can by using the SimpleDateFormat class directly. In Listing 2, we use the "MMMM d, yyyy" mask, which displays the full month, the day, and the full year.
Because the default was set in the tag, try modifying this tag without the custom formatting:
Notice that this outputs the date in the default format 07/29/2001.