[ LiB ] |
Designing your ColdFusion application generally starts when you have a particular problem to solve. For example, maybe you need to build a shopping cart or build a system to take care of your Human Resources information. You have one problem and a solution designed specifically for that problem. However, as your site grows and the functionality becomes more complex, having applications in multiple places that need access to the same data or that need to perform many of the same functions becomes a liability. You end up with more and more processing pages in different locations that all do the same thing. If you need to make a change, you end up having to change multiple locations.
One solution that ColdFusion provides is ColdFusion Components (CFCs). CFCs are files that encapsulate your application's information or functionality into separate files that can be maintained and accessed separately by multiple pages and applications.
For a detailed look at ColdFusion Components, check out Macromedia ColdFusion MX Web Application Construction Kit by Ben Forta et al.
Dreamweaver allows you to build CFCs quite easily by adding them as a document type that you can select from the New File dialog box. When you select Dynamic Page and ColdFusion Component, Dreamweaver opens a new page and inserts the necessary code framework:
<cfcomponent> <cffunction name="myFunction" access="public" returntype="string"> <cfargument name="myArgument" type="string" required="true"> <cfset myResult="foo"> <cfreturn myResult> </cffunction> </cfcomponent>
The CFCOMPONENT tag starts the file just like <html> starts an HTML file and indicates that the file is a CFC. Each CFC can contain multiple CFFUNCTION tags, each of which is a method of the component. Each CFFUNCTION can accept one or more arguments that are data passed to the function. It also returns some information that is handed back to the page that called the CFC.
A simple CFC would be one that gets a list of employees from a database and lists them with their departments. To make this interesting, though, the CFC also allows you to pass a variable from a form to limit the search to look for employees in a particular department. This is the CFC you will create in the exercise for this chapter.
You need to have ColdFusion MX installed on your machine, and the service needs to be running. You also need to have a web browser installed and running. You can test this by starting the ColdFusion administrator. In the Administrator, check the Data Sources. You should see that a data source named cfsnippets is installed. It comes with ColdFusion MX when you install it. If you see all that, you are set up correctly.
If you don't have a site set up for ColdFusion, set up one now. If you have a ColdFusion site defined already, you can skip to the next section.
You don't need any supporting files for this exercise. You will create the entire project from scratch, so you don't need to move any files around for this exercise.
Name SearchEmployeeExercise
Local Root Folder Create a new folder. Call it SearchEmployeeExercise. Set it as your local folder.
Server Type ColdFusion
Access Local/Network
Testing Server Folder Browse to the SearchEmployeeExercise folder.
URL Prefix http://localhost/SearchEmployeeExercise/.
<cfcomponent> <cffunction name="myFunction" access="public" returntype="string"> <cfargument name="myArgument" type="string" required="true"> <cfset myResult="foo"> <cfreturn myResult> </cffunction> </cfcomponent>
<cfcomponent> <cffunction name="GetEmployeeFunction" access="public" returntype="query"> <cfargument name="SearchString" required="No"> <cfquery name="SearchEmployees" datasource="cfsnippets"> SELECT * FROM employees ORDER BY LastName </cfquery> <cfreturn SearchEmployees> </cffunction> </cfcomponent>
<cfcomponent> <cffunction name="GetEmployeeFunction" access="public" returntype="query"> <cfargument name="SearchString" required="No"> <cfquery name="SearchEmployees" datasource="cfsnippets"> SELECT * FROM employees <cfif arguments.SearchString NEQ ""> WHERE Department LIKE '%#arguments.SearchString#%'</cfif> ORDER BY LastName </cfquery> <cfreturn SearchEmployees> </cffunction> </cfcomponent>
Now that you have created the CFC that executes the query and returns the result, you need to make the page that calls the CFC.
<html> <head> <title>Search Employees</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form name="form1" method="post" action=""> <p>Enter Department: <input name="department" type="text" id="department"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> <cfif IsDefined("Form.department")> <cfinvoke component="SearchEmployeeExercise.EmployeeDataCFC" method="GetEmployeeFunction" returnvariable="GetEmployeeFunctionRet"> </cfinvoke> </cfif> </body> </html>
<cfinvokeargument name="SearchString" value= "#Form.department#">
<b>#FirstName# #LastName#</b> works in #Department#<br>
Now you can test your application. When you preview the searchemployees.cfm file in the browser, you see the page with just the search box and the Submit button. When you call the page for the first time, no form value is defined. The CFIF condition tests and finds nothing, so the CFINVOKE is not called.
Enter nothing in the search box, and click Submit. The CFIF tests to see if a Form.department variable is defined. There is, even though it has no value. Because the variable is present, the CFINVOKE is called in the CFIF.
In the CFC, the CFARGUMENT is checked. The CFC finds that it is unnecessary to have an argument passed in, so the CFQUERY is executed. The CFQUERY has another CFIF that checks to see if the variable SearchString from the CFARGUMENT tag is empty. Because SearchString is empty, the CFIF does not evaluate as true. Therefore, the WHERE clause, which would limit the search to one department, is not executed. The CFQUERY finishes executing and passes that back as a query to the calling page, searchemployees.cfm .
On the calling page, the CFOUTPUT tag iterates through the returned data and prints the names of all the employees.
Now test the page and enter Sales for the department.
The CFIF tests to see if a Form.department variable is defined. Because the variable is present, the CFINVOKE is called in the CFIF.
In the CFC, the CFARGUMENT is checked. The CFC finds that it is unnecessary to have an argument passed in, so the CFQUERY is executed. The CFQUERY has another CFIF that checks to see if the variable SearchString from the CFARGUMENT tag is empty. Because SearchString has the value of Sales , the CFIF evaluates as true. Therefore, the WHERE clause, which limits the search to the one department specified, is executed. The CFQUERY finishes executing and passes that back as a query to the calling page, searchemployees.cfm .
On the calling page, the CFOUTPUT tag iterates through the returned data and prints the names of only the employees in the Sales department.
[ LiB ] |