Showing posts with label custom tag. Show all posts
Showing posts with label custom tag. Show all posts

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.

Wednesday, June 20, 2012

CFX(Java) Custom tags in ColdFusion-9(Part-1)

For  ColdFusion - 9 some sample java source files are present in “{ColdFusion_Root}/cfx\java\distrib\examples\” for windows and in “{ColdFusion_Root}/cfx/java/examples” directory for linux system.

<cfx_HelloColdFusion name="Mr. X">

Take the example of “CFX_HelloColdFusion” from the specified source codes present. If you directly execute that CFX tag with the above code without doing anything to the server you will get the following error message.

"Error processing CFX custom tag CFX_HelloColdFusion.
The CFX custom tag CFX_HelloColdFusion was not found in the custom tag database. You must add custom tags to the database before using them. If you have added your tag to the database, check the spelling of the tag within your template to ensure that it matches the database entry."

So you need to follow the following steps in order to install that source code as a CFX tag.

  1. Compile the source code ("HelloColdFusion.java") file  from the sample files of the ColdFusion installation  by using the command prompt or by using any IDE and generate the "HelloColdFusion.class" file.
  2. For testing the CFX tag place the  HelloColdFusion.class  file in “cf_root/wwwroot/WEB-INF/lib” in Server configuration and place in “cf_webapp_root/WEB-INF/lib” for J2EE configuration. In this place when you make any changes to the java code you only need to replace the new .class file with the existing one. You don't need to restart the ColdFusion server on each changes to the .class built.
  3. You can also place your .class files in your specific directory by changing the Class path settings in ColdFusion administrator.(Server Settings->Java and JVM -> ColdFusion Class Path). After that you need to restart your ColdFusion server.
  4. Then you need to register your CFX tag in ColdFusion admin:
  • Go to Extensions =>CFX Tags => Click Register Java CFX in ColdFusion admin. You will see a screen like below. 
  •  CFX tag name starts with the prefix "cfx_" then the class name of the java code in our case the class name is "HelloColdFusion". So the vale for tag name input will be "cfx_HelloColdFusion".
  •  In Class Name field we need to provide the class name without any .class extension. e.g-"HelloColdFusion".
  •  In Description text area we need to add some description for that tag. e.g-This tag shows greeting message. This field is Optional we can skip it. But, it helps us when we have very large no of CFX tags.
After all these steps if I will run the CFX code which I have written on the top of this page, I will get the following message as the output.
"Hello, Mr. X"

For more details on CFX(java)  in ColdFusion-9 have to wait for the next post. Thanks :)

1. CFX Custom Tag Part II

Followers