Adobe ColdFusion 8

cfcomponent

Description

Creates and defines a component object; encloses functionality that you build in CFML and enclose in cffunction tags. This tag contains one or more cffunction tags that define methods. Code within the body of this tag, other than cffunction tags, is executed when the component is instantiated.

A component file has the extension CFC and is stored in any directory of an application.

A component method is invoked in the following ways:

  • In the cfinvoke tag in a ColdFusion page
  • In a URL that calls a CFC file and passes a method name as a URL parameter
  • In the cfscript tag
  • As a web service
  • From Flash code

Category

Extensibility tags

Syntax

<cfcomponent
    bindingname = "binding element name"
    displayname = "string"
    extends = "component name"
    hint = "string"
    implements = "ColdFusion interface"
    namespace = "default service namespace"
    output = "no value|no|yes"
    porttypename = "port type element name"
    serviceaddress = "service URL"
    serviceportname = "port element name"
    style = "rpc|document"
    wsdlfile = "path">
    variable declarations
    <cffunction ...>
        ...
    </cffunction> 
    
    <cffunction ...>
        ...
    </cffunction> 
</cfcomponent>

See also

cfargument, cffunction, cfinterface, cfinvoke, cfinvokeargument, cfobject, cfproperty, cfreturn, IsInstanceOf, "Building and Using ColdFusion Components" in the ColdFusion Developer's Guide

History

ColdFusion 8:

  • Added the implements and serviceaddress attributes.
  • Added support for the onMissingMethod function.

ColdFusion MX 7:

  • Added support for publishing document-literal style web services.
  • Added the style, namespace, serviceportname, porttypename, wsdlfile, bindingname, and output attributes.
  • Extended functionality for the hint and displayname attributes when publishing document-literal style web services.

ColdFusion MX: Added this tag.

Attributes

Attribute

Req/Opt

Default

Description

bindingname

Optional

 

Specifies the binding attribute of the port element in the WSDL. If you don't specify this attribute, ColdFusion derives the value from the CFC class name.

displayname

Optional

 

A string that displays when you use introspection to show information about the CFC. The information appears on the heading, following the component name.

extends

Optional

WEB-INF.cftags.component

Name of parent component from which to inherit methods and properties. You can use the keyword component to specify the default value.

hint

Optional

 

Text that displays when you use introspection to show information about the CFC. The hint attribute value appears below the component name heading. Use this attribute to describe the purpose of the parameter.

implements

Optional

 

Name of the ColdFusion interface or interfaces that this component implements. If the component implements an interface, it must define all the functions in the interface, and the function definitions must conform to the definitions specified in the interface. For more information, see cfinterface.

A component can implement any number of interfaces. To specify multiple interfaces, use a comma-delimited list with the format interface1,interface2.

namespace

Optional

class name

Specifies the namespace used in the WSDL for a CFC that is invoked as a web service. If you don't specify this attribute, ColdFusion MX derives the value from the CFC class name.

output

Optional

Component body displayable text that is processed as standard CFML

Specifies whether constructor code in the component can generate HTML output; does not affect output in the body of cffunction tags in the component.

  • yes: Constructor code is processed as if it were within a cfoutput tag. Variable names surrounded by number signs (#) are automatically replaced with their values.
  • no: Constructor code is processed as if it were within a cfsilent tag.
  • If you do not specify this attribute, constructor code is processed as standard CFML. Any variables must be in cfoutput tags.

porttypename

Optional

 

Specifies the name attribute of the porttype element in the WSDL. If you don't specify this attribute, ColdFusion MX derives the value from the CFC class name.

serviceaddress

Optional

URL of the CFC

Specifies the SOAP URL of the web service. If you don't specify this attribute, ColdFusion MX uses the URL of the CFC in the WSDL service description. Use this attribute to specify the protocol, for example, by specifying a URL that starts with https://.

This attribute applies only for web services.

serviceportname

Optional

 

Specifies the name attribute of the port element in the WSDL. If you don't specify this attribute, ColdFusion MX derives the value from the CFC class name.

style

Optional

rpc

Specifies whether a CFC used for web services uses RPC-encoded style or document-literal style:

  • rpc: RPC-encoded style
  • document: Document-literal style

wsdlfile

Optional

 

A properly formatted WSDL file to be used instead of WSDL generated by ColdFusion.

Usage

If you specify the extends attribute, the data and methods of the parent component are available to CFC methods as if they were parts of the current component. If the managerCFC component extends the employeeCFC component, and the employeeCFC component has a getEmployeeName method, you can call this method by using the managerCFC, as follows:

<cfinvoke component="managerCFC" method="getEmployeeName" returnVariable="managerName" 
EmployeeID=#EmpID#>

This tag requires an end tag.

If you specify style="document", ColdFusion publishes the CFC as a document-literal style web service. For more information, see "Publishing document-literal style web services" in the ColdFusion Developer's Guide.

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:
Oct 21, 2008: Corrected

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

Example

<cfcomponent>
    <cffunction name="getEmp">
         <cfquery name="empQuery" datasource="cfdocexamples" >
             SELECT FIRSTNAME, LASTNAME, EMAIL
             FROM tblEmployees
         </cfquery>
         <cfreturn empQuery>
    </cffunction>

    <cffunction name="getDept">
        <cfquery name="deptQuery" datasource="cfdocexamples" >
             SELECT *
             FROM tblDepartments
         </cfquery>
         <cfreturn deptQuery>
    </cffunction>
</cfcomponent>