
Exercise 4: Writing structured, reusable code
63
<body>
<h1>Trip List</h1>
<cfoutput query="TripList">#tripName#<br></cfoutput>
</body>
</html>
2.
Save the file as triplisting.cfm in the my_app directory.
3.
View the triplisting.cfm page in a browser. The page lists all the trip names retrieved from the
Compass Travel database.
Reviewing the code
The following table describes the code used to display the query result set:
Exercise 4: Writing structured, reusable code
Generally, it is good coding practice to separate business logic from the display. The ColdFusion
page that you just created contains both business logic (the database query) and presentation (the
output code block). To separate them, you put the query in a ColdFusion component (CFC).
Doing so separates business logic from presentation; it also makes it easy to reuse the query
anywhere in your application. For more information, see Chapter 10, “Building and Using
ColdFusion Components” in
ColdFusion MX Developer’s Guide
.
To move the query to a CFC:
1.
Create the CFC file.
2.
Copy the query to the CFC.
3.
Call the method that contains the query.
Creating the CFC file
ColdFusion components (CFCs) are special files saved with the filename extension .cfc. They can
contain data and functions. Within CFCs, functions are referred to as
methods
. Actions that you
want ColdFusion to perform, such as querying a database, are contained in component methods.
One CFC can contain many methods. Each method in a CFC can return only one variable. The
following is the general syntax of a CFC:
<cfcomponent>
<cffunction name="firstMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
<cffunction name="secondMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
</cfcomponent>
Code Explanation
<cfoutput query="TripResult">
#tripName#<br</cfoutput>
Output code block. Displays the value of the tripName column
for each row in the result set from the "TripResult" query.
Содержание COLFUSION MX 7-GETTING STARTED BUILDING COLDFUSION...
Страница 1: ...COLDFUSION MX7 Getting Started Building ColdFusion MX Applications...
Страница 6: ...6 Contents...
Страница 10: ......
Страница 14: ...14 Chapter 1 Introducing ColdFusion MX...
Страница 38: ...38 Chapter 3 Database Fundamentals...
Страница 40: ......
Страница 58: ...58 Chapter 5 Lesson 2 Configuring Your Development Environment...
Страница 70: ...70 Chapter 6 Lesson 3 Retrieving Data...
Страница 84: ...84 Chapter 7 Lesson 4 Building Dynamic Queries...
Страница 96: ...96 Chapter 8 Lesson 5 Creating a Trip Detail Page...
Страница 102: ...102 Chapter 9 Lesson 6 Creating a Main Application Page...