Before writing some real business logic in a CFX tag we need to know the working and structure of CFX.
If we look into the java code of “HelloColdFusion.java” file present in “C:\ColdFusion9\cfx\java\distrib\examples” for ColdFusion developer installation in Windows machine then we will find the following text.
The first line of the code contains “import com.allaire.cfx.* ”. This means we are using some features of one existing java package(com.allaire.cfx). The jar file for package is present in “cf_root/wwwroot/WEB-INF/lib” for server configuration and present in “cf_webapp_root/WEB-INF/lib” for J2EE configuration.
In second line code it is just creating one public class called “HelloColdFusion”, it will be the name of our class in CFX tag and this class imports/extends another java interface called “CustomTag” which is present in “com.allaire.cfx” package. We are extending/importing that interface to get the functionality of that interface in our defined class “HelloColdFusion”.
In the next step we are creating one overloaded java method “processRequest”. The interface “CustomTag” contains one method, processRequest, which is passed Request and Response objects that are then used to do the work of the tag.
So, for each custom tag we must write the method “processRequest” and which is the start point for CFX tag execution. When we will call the CFX tag we don’t need to bother about the Request and Response Object from the ColdFusion side. This two Object holds information about what are the parameters passed to the custom tags and their properties and what are the response parameters passed back to ColdFusion and their properties.
I have listed the functions what we can access by the Request Object and their details. For more details click on the function.
|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.|
Followings are the response Object methods. For details about each method click on that method.
|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.|
Back to our code “String strName = request.getAttribute( "NAME" ) ” in this line we are just getting the value of the attribute “NAME” which we will pass during executing the CFX tag. Then, in the next line “response.write( "Hello, " + strName ) ” we are just writing some text back to the client. It will be display in the browser if we will run the page which contains that CFX through browser.
We can also return some variable to the calling page by writing the following code:
Now, you can compile your “.java” file to get “.class” file what we require.
In the next post we will see how we can implement some real business logic here and how we can pass one query object to custom tags and how we can return some query/Struct/variable from the custom tags.