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 
accessors = "yes|no" 
autoIndex = "yes|no" 
alias = "ActionScript 3 type alias" 
bindingname = "binding element name" 
displayname = "string" 
extends = "component name" 
hint = "string" 
implements = "ColdFusion interface" 
indexable = "yes|no" 
indexLanguage = "language"
initMethod = "method name"
mappedSuperClass = "yes|no" 
namespace = "default service namespace" 
output = "no value|no|yes"
persistent = "yes|no"
porttypename = "port type element name" 
Serializable = "yes|no" 
serviceaddress = "service URL" 
serviceportname = "port element name" 
style = "rpc|document|wrapped" 
wsversion = "1|2" 
wsdlfile = "path"> 
variable declarations 
<cffunction ...> 
... 
</cffunction> 

<cffunction ...> 
... 
</cffunction> 
</cfcomponent>

See also

cfargumentcffunctioncfinterfacecfinvokecfinvokeargumentcfobjectcfpropertycfreturnIsInstanceOf

Building and Using ColdFusion Components in the Developing ColdFusion Applications

History

ColdFusion 10: Added the attributes rest, restPath, httpMethod, produces, consumes, indexable, indexLanguage, autoIndex, wsVersion

ColdFusion 9.0.1: Added the attribute mappedSuperClass

ColdFusion 9: Added the attributes initMethod, serializable and accessors.

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

accessors

Optional

yes (for persistent CFC)no for others

If set to yes, lets you invoke implicit getters and setters.
For persistent CFC, accessors is always enabled.

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.

autoIndex

Optional

yes

If no, auto-indexing of CFC does not occur. That is, indexing occurs only in offline mode.

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.

consumes

optional

/

Comma-separated list of acceptable MIME types, for example consumes="text/plain,text/html".Searches for the Content-Type header in the HTTP request; if the value is, for example HTML or XML, ColdFusion finds the appropriate method which can handle the request and invoke the method.Used to specify which MIME media types of representation a resource can accept or consume, from the client. If this attribute is used at the component level (and not at the function level), all the response methods accept the specified MIME types by default.
If no function in a CFC can consume the MIME type in a client request, the following error occurs: 415 Unsupported Media Type.
If no value is specified, / is taken by default, which means, all MIME types are consumed.

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.

httpMethod

optional

 

The HTTP method to use, must be one of the following:

  • GET: Requests information from the server. Any data that the server requires to identify the requested information must be in the URL or in cfhttpparam type="URL" tag.
  • POST: Sends information to the server for processing. Requires one or more cfhttpparam tags. Often used for submitting form-like data.
  • PUT: Requests the server to store the message body at the specified URL. Use this method to send files to the server.
  • DELETE: Requests the server to delete the specified URL.
  • HEAD: Identical to the GET method, but the server does not send a message body in the response. Use this method for testing hypertext links for validity and accessibility, determining the type or modification time of a document, or determining the type of server. If you have not specified HEAD, by default, GET method is called. However, message body is not sent in the response.
  • OPTIONS: A request for information about the communication options available for the server or the specified URL. This method enables the ColdFusion application to determine the options and requirements associated with a URL, or the capabilities of a server, without requesting any additional activity by the server. If you have not specified OPTIONS, then ColdFusion sends a response.
    Based on the request method, the corresponding function is invoked. For example, if a GET request is sent to the server, then a function with httpMethod as GET is invoked.
    If this value is not specified at the function level, then the value you define here is used. So all functions for which httpMethod is not specified are assigned this value.

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.

indexable

Optional

no

If yes, enables indexing for the component.

indexLanguage

Optional

english

Specify the language that is used to index and search.
The value you set overrides the value defined in the Application.cfc.

initMethod Optional init Allows the specification of a different method to use for the component's constructor.

mappedSuperClass

Optional

no

If set to yes on a non-persistent CFC, child CFCs can inherit its properties. For example, you can define a base CFC with common properties such as ID, version, or createdOn which all other persistent CFCs would extend and thus get one common behavior.mappedSuperClass cannot be set to yes on a persistent CFC.

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 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.

persistent

Optional

 

Specifies if the CFC is persistent or not.

porttypename

Optional

 

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

produces

optional

/

Comma-separated list of acceptable MIME types, for example produces="text/plain,text/html". Searches for the Accept header in the HTTP request or extension in the URL (valid extensions are .xml and .json); if the value is, for example HTML, JSON, or XML, ColdFusion finds the appropriate method that can handle the request and invoke the method.Used to specify the MIME media types or representations a CFC can produce and send back to the client.
If specified at the component level (and not at the function level) , all the functions in a CFC can produce the specified MIME types by default. If no methods in a CFC can produce the MIME type in a client request, the following error occurs: 406 Not Acceptable.
If no value is specified, / is taken by default, which means, all MIME types are produced.

rest

Optional

true if RestPath is specified

If true, the CFC is REST-enabled.

restPath

Optional

 

You can access the REST resource either by providing a REST path or CFC path. So specify if you want to use a path other than the CFC path. If rest="true" and you do not specify this attribute, path to the CFC is used.Path to the CFC should be from the directory registered as a REST service. Also, you should include the CFC name in the path. For example, if you have a folder college that is registered as RestTest, and you want to publish student.cfc which is in a sub-folder department (in the folder college), then the URL used to access student.cfc is as follows:

http://localhost:8500/rest/RestTest/department/student

.
The restPath in this case is department/student.
The path is case-sensitive. Also, it is preferable to avoid special character in the path.At CFC level, specify the path as follows:restPath="restService" or {{restPath="test/restService"}}You
can specify templates as value of restPath. For
example, restPath="{name}". This means, all URLs
in the format <Base URL>/rest/<service_mapping>/<any string without slash> matches the restPath. For example,

http://localhost:8500/rest/service1/abc

.
The value in place of template can be accessed using the restargsource value path. For details, see the attribute details of cfargument.
The attribute can have complex expressions as values. For example, restpath="customers/{firstname}-{lastname}".
This matches URLs such as "/customers/paul-bensen" but
not "/customers/paulbensen".Also restPath can
include regular expressions such as restpath="/customers/{id : d+}".
Here, id is an integer that can match "/customers/123111" but
not "/customers/asd" or "/customers/123/122".
If you have two methods with different path attribute values and if the
paths are ambiguous, then there are precedence rules for finding
the match. For example, restpath="/customers/{id : .+}".
This expression matches any stream of characters after /customer.
It matches "/customers/123", "/customers/asd",
and "/customers/123/12/asdfa", but not "/customers".
You can also have expressions such as restPath="/customers/{id : .+}/address", the
URL "/customers/123/asd/address" is matched with
both URLs. In such scenarios, the precedence rules are used to find
the match.

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 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 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
  • wrapped: If you are setting wsVersion as 2, the default value is wrapped and if it is 1, then the default value is rpc.

wsdlfile

Optional

 

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

wsVersion

Optional

 

If you specify 2, CFC is deployed using Axis 2 engine. The value you specify overrides the value you specify at application or server level.

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 onMissingMethodfunction 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 1

<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>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy