32 CHAPTER 30 Extending ColdFusion with CFX
Listing 30.5 (continued)
// case of an unexpected exception)
catch( ... )
{
pRequest->ThrowException(
“Error occurred in tag CFX_COMPUTESTANDARDDEVIATION”,
“Unexpected error occurred while processing tag.” ) ;
}
}
As you can see, the basic skeleton of the ProcessTagRequest() function has not been changed from
the code generated by the Tag Wizard (Listing 30.5). An additional
#include directive for the stan-
dard
math.h library has been added at the top of the listing; all the other changes are within the
body of the
try block within the ProcessTagRequest() function. The ProcessTagRequest() function
serves the same purpose as the
processRequest() method in the Java interface; basically, ColdFusion
will call this function each time the CFX tag is actually used in a ColdFusion page. Think of this
function as being similar conceptually to the ubiquitous
main() function that would appear in a
standalone C++ program.
NOTE
Actually, you can change the name of the ProcessTagRequest() function if you wish, as long as you specify the same name in
the Procedure field when you register the tag with the ColdFusion Administrator (discussed in the next section). You can even create
several custom tags in a single C++ file (and thus in one compiled .dll file), simply by including several functions that have the same
basic form as
ProcessTagRequest() (including the try and catch blocks as shown in Listing 30.5). However, the custom-
ary thing is to leave the name of the function alone and simply create each custom tag as a separate C++ project. I recommend that
you do the same unless there is some specific reason why you really want them all compiled into the same DLL.
At the top of ProcessTagRequest(), the AttributeExists() and GetAttribute() methods are used
to make sure that the appropriate attributes have been passed to the tag (refer back to Table 30.13).
If an attribute is missing, the
ThrowException() method is used to throw a ColdFusion-style excep-
tion, which in turn displays an error message in the calling page (unless the exception is caught with
<cftry> and <cfcatch>).
NOTE
Please refer to the “A Word on Exceptions” section, earlier in this chapter, for a few additional notes on throwing exceptions from
CFX tags.
Some of the attribute validation code at the top of this listing involves making sure that a valid
ColdFusion query has been passed to the tag. As explained near Table 30.7 (near the beginning of
this chapter), the only way to pass a query to a CFX tag is to provide a
query attribute to the tag; the
query is then accessible to the tag via the
pRequest->GetQuery() method, as shown in Listing 30.6.
Once
GetQuery() has been used to get a reference to the query, the GetRowCount(), GetData(), and
other methods listed in Table 30.7 can be used to retrieve or modify the data in the query. For instance,
this tag uses pQuery->GetColumns()->GetIndexForString() to verify that the column name specified
in the
COLUMN attribute of the tag actually exists in the query that was passed to the tag. It also uses
pQuery->GetRowCount() to make sure that the query includes at least one row.
Commenti su questo manuale