Sunday, December 16, 2012

Exploring CFX(Java) in ColdFusion-9(Part-2)

NOTE: Please take a look into this URL to start from beginning.

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.
Request Object:

MethodDescription
attributeExistsChecks whether the attribute was passed to this tag.
debugChecks whether the tag contains the debug attribute.
getAttributeRetrieves the value of the passed attribute.
getAttributeListRetrieves a list of all attributes passed to the tag.
getIntAttributeRetrieves the value of the passed attribute as an integer.
getQueryRetrieves the query that was passed to this tag, if any.
getSettingRetrieves the value of a global custom tag setting.


Response Object:
Followings are the response Object methods. For details about each method click on that method.

MethodDescription
writeOutputs text to the calling page.
setVariableSets a variable in the calling page.
addQueryAdds a query to the calling page.
writeDebugOutputs 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:
response.setVariable("Status", "true");

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.

No comments:

Post a Comment

Followers