onMissingMethod Example

CFCs support an onMissingMethod function. By defining an onMissingMethod function in the cfcomponent tag body in the CFC, you can handle calls to methods that are not implemented in the CFC. If an application calls a function that is not defined in the CFC, ColdFusion calls the onMissingMethod function and passes it the requested method's name and arguments.

If you do not define an onMissingMethod function, a call to a method that is not defined in the CFC causes ColdFusion to throw an error that must be handled in the calling code.

The onMissingMethod function is useful for several purposes: To handle errors directly in the component, instead of requiring that each instance of code that calls the component handles them. To create a dynamic proxy, an object that can take arbitrary calls and dynamically determines the correct action.

The onMissingMethod function must have the following format:

<cffunction name="onMissingMethod">
<cfargument name="missingMethodName" type="string">
<cfargument name="missingMethodNameArguments" type="struct">
code to handle call to nonexistent method
</cffunction>

The following example shows usage of onMissingMethod.

This is the code for cfc:

<cffunction name="Sum">
<cfargument name="x" required="true">
<cfargument name="y" required="true">

<cfset total = x + y>
<cfreturn total>
</cffunction>

<cffunction name="OnMissingMethod">

<cfoutput><br><br>*****************************************************<br></cfoutput>
<cfoutput>Hello, Welcome to cf-examples.net...<br>
You are trying to call the method "#arguments.missingMethodName#" with parameters:
#arguments.missingmethodarguments.1# and #arguments.missingmethodarguments.2#
<br> This method does not exist.<br><br>
This is from the onMissingMethod.</cfoutput>
<cfoutput><br><br>*****************************************************<br></cfoutput>

</cffunction>

Following below is the cfm code:

<!--- create an object of the mytest cfc --->
<cfset mytestobj = createobject("component", "mytest")>

<!--- call the method sum with arguments --->
<cfset result = mytestobj.sum("5", "2")>

<!--- output the obtained result --->
<cfoutput>The result value is: #result#</cfoutput>

<!--- Try to call a method which does not exist. The control should go to the onMissingMethod in the cfc. --->
<cfset result = mytestobj.multiply("5", "2")>

You can download the source code using the download button.

Here are a few useful links related to onMissingMethod.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_10.html

http://www.bennadel.com/blog/868-Learning-ColdFusion-8-OnMissingMethod-Event-Handler.htm

http://www.coldfusionjedi.com/index.cfm/2008/6/13/Ask-a-Jedi-Problem-using-onMissingMethod-inside-a-CFC

http://www.coldfusionjedi.com/index.cfm/2007/8/5/Warning-about-onMissingMethod

cffeed example

CFFeed Description: Reads or creates an RSS or Atom syndication feed. This tag can read RSS versions 0.90, 0.91, 0.92, 0.93, 0.94, 1.0, and 2.0, and Atom 0.3 or 1.0. It can create RSS 2.0 or Atom 1.0 feeds.

The following cffeed example shows usage of cffeed.

<!--- read in a struct --->
<cffeed action="read" source="atom1.xml" name="myfeed">
<cfoutput>The Feed output from atom1.xml is as below:</cfoutput>
<cfdump var="#myfeed#"><br>

<!--- read in a query --->
<cffeed action="Read" source="atom1.xml" query="feedQuery" >
<br>
<cfoutput>The Feed output from atom1.xml is as below:</cfoutput>
<cfdump var="#myfeed#"><br>

You can download the source using the download button.

structnew example

The following example shows usage of StructNew.

<cfset employee = StructNew()>

<cfset employee.first = "FirstName">
<cfset employee.last = "LastName">
<cfset employee.value1 = "12345">

<!--- Nested Structure... Have a structure inside a structure. --->
<cfset addr = structnew()>
<cfset addr.nu = "10">
<cfset addr.street = "BG Road">
<cfset addr.city = "Bangalore">

<cfset employee.address = "#addr#">

<cfdump var = #employee#>

You can download the source using the download button.

cfexchange - get messages example

This is an Example showing the usage of cfexchange tags.This example shows how to connect and get messages from the server. Note: You need to update the Application.cfm file with the appropriate values for exchange server, user ids, password, email ids.

<cftry>
<!--- Send an email, to verify --->
<cfmail to = "#email1#"   
      from = "#email2#"   
      subject = "Testing cfexchange message 1"   
      username = "#user1#"   
      password = "#password#"   
      server = "#exchangeServerIP#"   
      port = "25">

This message is sent by cfexchange test.
</cfmail>

<!--- Sleep for sometime so that the message is transferred/sent to the server. ---->
<cfscript>sleep(15000);</cfscript>

<!--- Connect to the server using cfexchangeconnection. ---->
<cfexchangeConnection
action="open"
username ="#user1#"
password="#password#"
server="#exchangeServerIP#"
connection="testconn1">


<!--- Get the mails from the server using cfexchangemail. ---->
<cfexchangeMail action="get" connection="testconn1" name="getMessages">
<cfExchangeFilter name="subject" value="message 1">
</cfexchangeMail>

<cfdump var="#getMessages#">

<!--- Close the connection. ---->
<cfexchangeConnection
action="close"
connection="testconn1">

<cfcatch>
   <cfoutput>#CFCATCH.message#</cfoutput>
</cfcatch>
</cftry>

You can download the source files using the download button.

Extract Text From PDF

Here is an Example showing the extraction of text from a pdf file, using ddx. The extracted text will be in the destination file.

<!--- The ddx file --->
<cfset ddxfile = Expandpath("doc_text.ddx")>
<!--- The source pdf file --->
<cfset sourcefile1 = Expandpath("pdf-file1.pdf")>
<!--- The destination file --->
<cfset destinationfile = Expandpath("ddx_result_doc_text.xml")>

<cfset inputStruct=StructNew()>
<cfset inputStruct.Doc1="#sourcefile1#">

<cfset outputStruct=StructNew()>
<cfset outputStruct.Out1="#destinationfile#">

<cfpdf action="processddx" ddxfile="#ddxfile#" inputfiles="#inputStruct#" outputfiles="#outputStruct#" name="ddxVar">

<cfoutput>The ddx operation was #ddxVar.Out1#</cfoutput><br>

<cfif #ddxVar.Out1# eq "successful">
   <cffile action="read" file="#destinationfile#" variable="filedata">
<cfdump var="#filedata#">
</cfif>

You can download the files using the download button.

URLEncodedFormat example

Following is an example to show the usage of the function "URLEncodedFormat".

Description: Generates an URL-encoded string. For example, it replaces spaces with %20, and non-alphanumeric characters with equivalent hexadecimal escape sequences.

Returns: A copy of a string, URL-encoded.

syntax: URLEncodedFormat(string [, charset ])

string: A string or a variable that contains a string.

charset: The character encoding in which the string is encoded. Optional.

The following list includes commonly used values:

* utf-8 * iso-8859-1 * windows-1252 * us-ascii * shift_jis * iso-2022-jp * euc-jp * euc-kr * big5 * euc-cn * utf-16

Here is a simple example:

<cfset enc = URLEncodedFormat("Adobe Cold Fusion 8")>
<br><cfoutput>The URLEncodedFormat of "Adobe Cold Fusion 8" is <br>"#enc#".
</cfoutput>

<br><cfset enc = URLEncodedFormat("JRUN&ColdFusion 8.0.1")>
<cfoutput>The URLEncodedFormat of "JRUN&ColdFusion 8.0.1" is <br>"#enc#".
</cfoutput>

Another example follows:

<h3>URLEncodedFormat Example</h3>
<cfif IsDefined("url.myExample")>
<p>The url variable url.myExample was passed from the previous link ...
its value is:
<br><b>"<cfoutput>#url.myExample#</cfoutput>"</b>
</cfif>
<p>This function returns a URL encoded string.
<cfset s = "My url-encoded string has special characters & other stuff">
<p> <A HREF = "urlencodedformat.cfm?myExample=<cfoutput>#URLEncodedFormat(s)#
</cfoutput>"
>Click me</A>

You can download the code using the download button.

cfchart embedded inside cfdocument

Example to show the usage of cfchart embedded inside cfdocument.

Description: Embed a cfchart inside a cfdocument. Note: In case of cfcharts, if you want to embed a cfchart inside a cfdocument, you can use the following formats for cfchart. 1. jpg 2. png Currently, format="flash" is NOT supported.

You should have the datasource cfdocexamples(this comes by default with the installation)

<!---The following example analyzes the salary data in the cfdocexamples
database and generates a bar chart showing average salary by department. The
body of the cfchartseries tag includes one cfchartdata tag to include data that
is not available from the query. --->


<cfdocument format="pdf">

<!--- Get the raw data from the database. --->
<cfquery name="GetSalaries" datasource="cfdocexamples">
SELECT Departmt.Dept_Name,
Employee.Dept_ID,
Employee.Salary
FROM Departmt, Employee
WHERE Departmt.Dept_ID = Employee.Dept_ID
</cfquery>

<!--- Use a query of queries to generate a new query with statistical data for each department.
AVG and SUM calculate statistics. GROUP BY generates results for each department. --->

<cfquery dbtype="query" name="DataTable">
SELECT
Dept_Name,
AVG(Salary) AS avgSal,
SUM(Salary) AS sumSal
FROM GetSalaries
GROUP BY Dept_Name
</cfquery>

<!--- Reformat the generated numbers to show only thousands. --->
<cfloop index = "i" from = "1" to = "#DataTable.RecordCount#">
      <cfset DataTable.sumSal[i] = Round(DataTable.sumSal[i]/1000)*1000>
<cfset DataTable.avgSal[i] = Round(DataTable.avgSal[i]/1000)*1000>
</cfloop>

<h1>Employee Salary Analysis</h1>
<!--- Bar graph, from Query of Queries --->
<cfchart format="jpg" xaxistitle="Department" yaxistitle="Salary Average">
<cfchartseries type="cone" query="DataTable" itemcolumn="Dept_Name" valuecolumn="avgSal">
    <cfchartdata item="Facilities" value="35000">
</cfchartseries>
   </cfchart>

</cfdocument>

You can download the example code using the download link.

cfdirectory single character wild card example

cfdirectory single character wild card example.

Here is an example to show the usage of cfdirectory filtering using single character wild card(?).

Description: cfdirectory manages interactions with directories.

Syntax

<cfdirectory
directory = "directory name"
action = "list|create|delete|rename"
filter = "list filter"
listInfo = "name|all"
mode = "permission"
name = "query name"
newDirectory = "new directory name"
recurse = "yes|no"
sort = "sort specification"
type = "file|dir|all">

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

<!--- List all files in the current directory based on the following filters:
       Get files whose filenames have only FOUR characters followed by the 'cfm' extension type. ex:test.cfm, fun1.cfm etc
--->

<cfset filters = "????.cfm">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="Files with four chars">
<br>

<!--- Multiple filters:
       Get files whose filenames have only FOUR characters followed by the 'cfm' extension type. ex:test.cfm, fun1.cfm etc
       And
       Get files whose filenames have only THREE characters followed by the 'cfm' extension type. ex:fun.cfm
       And
       Get ALL files with .cfc extension. ex:app.cfc
--->

<cfset filters = "????.cfm|???.cfm|*.cfc">
<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="Multiple Filters">
<br>

You can download the sample cfm file using the download button.

cfdirectory filters example

Example to show the usage of cfdirectory with filters.

Description: cfdirectory manages interactions with directories.

Syntax

<cfdirectory
directory = "directory name"
action = "list|create|delete|rename"
filter = "list filter"
listInfo = "name|all"
mode = "permission"
name = "query name"
newDirectory = "new directory name"
recurse = "yes|no"
sort = "sort specification"
type = "file|dir|all">

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

<!--- List all files in the current directory based on the following filters(single filter). --->
<cfset filters = "*.cfm">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="All cfm files">
<br>

<!--- List all files in the current directory based on the following filters(multiple filter). --->
<cfset filters = "*.cfm|*.cfc">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="cfm and cfc files">
<br>

cfdirectory example

Following is an example to show the usage of cfdirectory.

<!--- List all files in the current directory. --->
<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="All Files">
<br>

More Entries

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner