11Writing CFX Tags with Java
Basically, every Java CFX tag needs to be a public class that implements the CustomTag interface in
the
com.allaire.cfx package. There isn’t a whole lot to the CustomTag interface; you can think of it
as a simple agreement which says that every custom tag must include a
processRequest() function
like the one shown in Listing 30.1. Each time the CFX tag is used in a ColdFusion page, the server
will invoke the
processRequest() method, passing information about the current page request to
the
request and response arguments. (The guts of the tag can then call the various methods exposed
by
request and response, as listed in the tables from the first part of this chapter.)
When you add these modest requirements up, it means that every Java CFX tag must include the
following code skeleton, nearly verbatim. The only thing that will change from tag to tag is the
name of the class (here, it’s
HelloWorld):
import com.allaire.cfx.* ;
public class HelloWorld implements CustomTag {
public void processRequest( Request request, Response response )
throws Exception
{
...CFX tag logic goes here...
}
}
The remainder of Listing 30.1 is simple. The request.attributeExists() method from Table 30.2
is used to make sure that the
NAME and AGE attributes are passed to the tag each time it is used. If not,
the standard Java
throw statement is used to create an exception, which will bubble up to the Cold-
Fusion server for display on the calling page.
Assuming the attributes have been provided, execution proceeds to the
response.write() lines. As
explained in Table 30.4, the
response.write() method inserts any string into the current ColdFusion
page, much like
<cfoutput> or WriteOutput() in CFML (or response.getWriter().write() in a Java
Servlet). In this case, the strings are a combination of static text and the actual values of the
NAME and
AGE attributes, available from the request.getAttribute() method (see Table 30.2).
That’s it!
Compiling the CFX Tag
With the Java code written, the next step is to compile it into a Java class file. If you’re using a dedi-
cated Java IDE, you can probably just hit a Compile button on some kind of toolbar, but for this
discussion I’ll assume you need to compile the class manually using Sun’s
javac compiler.
If you don’t have a copy of Sun’s Java SDK (also known as the JDK) on your system, you will need
to download one now. You can download the SDK at no charge from
http://java.sun.com. Go
ahead and run the installation program, making a mental note of the directory that the SDK gets
installed into as you go.
NOTE
You can use any modern version of the SDK to compile your CFX tags, but for consistency’s sake I would recommend using a version
equal to (or later than) the version of the JRE that ColdFusion is using as its runtime engine. For the initial release of ColdFusion, that
means version 1.3.1_01 or later. If you use a later version, just keep in mind that ColdFusion won’t be able to run the CFX tag if it uses
built-in classes that aren’t supported by the version of the JRE that ColdFusion is using.
Commenti su questo manuale