Create a source file in your editor with the following code:
To create a Java CFX tag, create a class that implements the Custom tag interface. This interface contains one method, processRequest, which is passed Request and Response objects that are then used to do the work of the tag.
The example in the following procedure creates a simple Java CFX tag named cfx_MyHelloColdFusion that writes a text string back to the calling page.
-
public class MyHelloColdFusion implements CustomTag { public void processRequest( Request request, Response response ) throws Exception { String strName = request.getAttribute( "NAME" ) ; response.write( "Hello, " + strName ) ; } }
-
Save the file as MyHelloColdFusion.java in the WEB_INF/classes directory.
-
Compile the java source file into a class file using the Java compiler. If you are using the command line tools bundled with the JDK, use the following command line, which you execute from within the classes directory:
javac -classpath cf_root\WEB-INF\lib\cfx.jar MyHelloColdFusion.java
The previous command works only if the Java compiler (javac.exe) is in your path. If it is not in your path, specify the fully qualified path; for example, c:\jdk1.3.1_01\bin\javac in Windows or /usr/java/bin/javac in UNIX.
If you receive errors during compilation, check the source code to make sure that you entered it correctly. If no errors occur, you successfully wrote your first Java CFX tag.
Calling the CFX tag from a ColdFusion page
You call Java CFX tags from within ColdFusion pages by using the name of the CFX tag that is registered on the ColdFusion Administrator CFX Tags page. This name should be the prefix cfx_ followed by the class name (without the .class extension).
Register a Java CFX tag in the ColdFusion Administrator
-
In the ColdFusion Administrator, select Extensions > CFX Tags.
-
Click Register Java CFX.
-
Enter the tag name (for example, cfx_MyHelloColdFusion).
-
Enter the class name without the .class extension (for example, MyHelloColdFusion).
-
(Optional) Enter a description.
-
Click Submit.
You can now call the tag from a ColdFusion page.
Call a CFX tag from a ColdFusion page
-
Create a ColdFusion page (.cfm) in your editor with the following content to call the HelloColdFusion custom tag:
<body> <cfx_MyHelloColdFusion NAME="Les"> </body> </html>
-
Save the file in a directory configured to serve ColdFusion pages. For example, you can save the file as C:\inetpub\wwwroot\cfdocs\testjavacfx.cfm in Windows or /home/docroot/cfdocs/testjavacfx.cfm in UNIX.
-
If you have not already done so, register the CFX tag in the ColdFusion Administrator (see Registering CFX tags in Developing CFX tags in C++).
-
Request the page from your browser using the appropriate URL; for example: //localhost/ cfdocs /testjavacfx.cfm
ColdFusion processes the page and returns a page that displays the text "Hello, Les." If an error is returned instead, check the source code to make sure that you entered it correctly.
Delete a CFX tag in the ColdFusion Administrator
-
In the ColdFusion Administrator, select Extensions > CFX Tags.
-
For the tag to delete, click the Delete icon in the Controls column of the Registered CFX Tags list.
Processing requests
Implementing a Java CFX tag requires interaction with the Request and Response objects passed to the processRequest method. In addition, CFX tags that must work with ColdFusion queries also interface with the Query object. The com.allaire.cfx package, located in the WEB-INF/lib/cfx.jar archive, contains the Request, Response, and Query objects.
For a complete description of these object types, see ColdFusion Java CFX Reference in the CFML Reference. For a complete example Java CFX tag that uses Request, Response, and Query objects, see ZipBrowser example.
Request object
The Request object is passed to the processRequest method of the CustomTag interface. The following table lists the methods of the Request object for retrieving attributes, including queries, passed to the tag and for reading global tag settings:
Method |
Description |
---|---|
attributeExists |
Checks whether the attribute was passed to this tag. |
debug |
Checks whether the tag contains the debug attribute. |
getAttribute |
Retrieves the value of the passed attribute. |
getAttributeList |
Retrieves a list of all attributes passed to the tag. |
getIntAttribute |
Retrieves the value of the passed attribute as an integer. |
getQuery |
Retrieves the query that was passed to this tag, if any. |
getSetting |
Retrieves the value of a global custom tag setting. |
For detailed reference information on each of these interfaces, see the CFML Reference.
Response object
The Response object is passed to the processRequest method of the CustomTag interface. The following table lists the methods of the Response object for writing output, generating queries, and setting variables within the calling page:
Method |
Description |
---|---|
write |
Outputs text to the calling page. |
SetVariable |
Sets a variable in the calling page. |
addQuery |
Adds a query to the calling page. |
writeDebug |
Outputs text to the debug stream. |
For detailed reference information on each of these interfaces, see the CFML Reference.
Query object
The Query object provides an interface for working with ColdFusion queries. The following table lists the methods of the Query object for retrieving name, row count, and column names and methods for getting and setting data elements:
Method |
Description |
---|---|
getName |
Retrieves the name of the query. |
getRowCount |
Retrieves the number of rows in the query. |
getColumnIndex |
Retrieves the index of a query column. |
getColumns |
Retrieves the names of the query columns. |
getData |
Retrieves a data element from the query. |
addRow |
Adds a new row to the query. |
setData |
Sets a data element within the query. |
For detailed reference information on each of these interfaces, see CFML Reference.
Life cycle of Java CFX tags
A new instance of the Java CFX object is created for each invocation of the Java CFX tag. As a result, it is safe to store per-request instance data within the members of your CustomTag object. To store data and objects that are accessible to all instances of your CustomTag, use static data members. If you do so, ensure that all accesses to the data are thread-safe.