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

Syntax

<cfcomponent 
    alias = "ActionScript 3 type alias" 
    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" 
    Serializable = "yes|no" 
    serviceaddress = "service URL" 
    serviceportname = "port element name" 
    style = "rpc|document" 
    wsdlfile = "path"> 
    variable declarations 
    <cffunction ...> 
        ... 
    </cffunction>  
     
    <cffunction ...> 
        ... 
    </cffunction>  
</cfcomponent>

History

ColdFusion 9: Added the serializable attribute.

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

alias

Optional

Specifies the type label to give the object when it is converted from CFML to ActionScript 3. It matches the alias attribute of AS3 types. This is attribute applies only to Flash Remoting and LiveCycle Data Services value objects, and lets you work with typed objects in both ColdFusion and Flash.

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.

serializable

Optional

true

Specifies whether this component can be serialized. If you set this value to false, the component and the data in the component’s This and Variables scopes cannot be serialized, so they are not retained on session replication, and the component is in its default state.

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 Developing ColdFusion Applications.

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="missingMethodArguments" type="struct"> 
        code to handle call to nonexistent method 
</cffunction>
Note: The argument name for onMissingMethod must not change.

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>