ColdFusion 9.0 Resources |
Writing a Java CFX tagTo 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.
Note: 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 pageYou 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
You can now call the tag from a ColdFusion page. Call a CFX tag from a ColdFusion page
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. Processing requestsImplementing 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 objectThe 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:
For detailed reference information on each of these interfaces, see the CFML Reference. Response objectThe 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:
For detailed reference information on each of these interfaces, see the CFML Reference. Query objectThe 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:
For detailed reference information on each of these interfaces, see CFML Reference. Life cycle of Java CFX tagsA 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. |