-
Getting Started Building ColdFusion MX Applications
-
Trademarks Afterburner, AppletAce, Attain, Attain Enterprise Learning System, Attain Essentials, Attain Objects for Dreamweaver, Authorware, Authorware Attain, Authorware Interactive Studio, Authorware Star, Authorware Synergy, Backstage, Backstage Designer, Backstage Desktop Studio, Backstage Enterprise Studio, Backstage Internet Studio, ColdFusion, Design in Motion, Director, Director Multimedia Studio, Doc Around the Clock, Dreamweaver, Dreamweaver Attain, Drumbeat, Drumbeat 2000, Extreme 3D, Fireworks,
-
CONTENTS INTRODUCTION ................................................... 7 About Macromedia ColdFusion MX documentation . . . . . . . . . . . . . . . . . . . . . . . 7 Documentation set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Viewing online documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 PART I: Welcome to ColdFusion CHAPTER 1: Introducing ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . .
-
CHAPTER 3: Database Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Understanding database basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 What is a relational database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Understanding relational tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 About SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
CHAPTER 7: Lesson 3: Creating a Main Application Page . . . . . . . . . . . . . . . . . 77 Enhancing the Trip Maintenance application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Showing additional trip details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Avoiding the potential security risk when using dynamic SQL . . . . . . . . . . . . . 84 Linking the Search Results page to the Trip Detail page . . . . . . . . . . . . . . . . . .
-
Contents
-
INTRODUCTION Getting Started Building ColdFusion MX Applications is intended for anyone who needs to begin programming in the ColdFusion MX development environment. This book includes a tutorial, which uses supporting files that are installed if you chose to install example applications. If you did not install the ColdFusion MX example applications, you can download the tutorial files from the ColdFusion Support center on the Macromedia website.
-
Viewing online documentation All ColdFusion MX documentation is available online in HTML and Adobe Acrobat Portable Document Format (PDF) files. Go to the documentation home page for ColdFusion MX on the Macromedia website: www.macromedia.com.
-
PART I Welcome to ColdFusion Part I provides an introduction to ColdFusion. It defines ColdFusion and provides an overview of the ColdFusion Markup Language (CFML). It also provides generic database concepts, and information about how to prepare your development environment for using the tutorial in Part II of this book. Introducing ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 CFML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
-
CHAPTER 1 Introducing ColdFusion MX This chapter introduces the core technologies that are the foundation for Macromedia ColdFusion MX. In addition, it introduces the basic concepts about ColdFusion MX, how it works, and the various components that comprise it. Contents What is ColdFusion MX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Using ColdFusion MX with Macromedia Flash MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
What is ColdFusion MX? ColdFusion MX is a powerful web application server that lets you create robust sites and applications without a long learning curve. ColdFusion MX does not require coding in traditional programming languages (for example, C/C++, Java, XML), although it supports these traditional programming languages.
-
Interacting with data sources ColdFusion applications can interact with any database that supports a JDBC technology-based driver. A JDBC technology-based driver uses an Application Programming Language (API) to execute SQL statements to databases on most platforms. However, ColdFusion is not limited to JDBC data sources. You can also interact with existing Open Database connectivity (ODBC) data sources by using ODBC Socket, a driver that interacts with an existing ODBC driver.
-
Using ColdFusion MX with Macromedia Flash MX Macromedia Flash MX is designed to overcome the many limitations of HTML and solve the problem of providing efficient, interactive, user interfaces for Internet applications. ColdFusion MX is designed to provide a fast, efficient environment for developing and providing data-driven Internet applications. By using ColdFusion MX and Flash MX together, you can easily create complete visually interactive applications for the Internet.
-
CHAPTER 2 CFML Basics This chapter introduces the basic elements of CFML, including how to create ColdFusion pages, and use variables, functions, conditional processing, and form processing. Contents Working with ColdFusion pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Understanding CFML elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Working with CFML expressions. . . . . . . . . . . . . . .
-
Working with ColdFusion pages As discussed in Chapter 1, “Introducing ColdFusion MX,” on page 11, ColdFusion pages are plain text files that you use to create web applications. You can create your ColdFusion applications by writing all the code manually or by using wizards (provided with some editors) to generate the majority of the code for you.
-
To create a ColdFusion page: 1 Open your editor and create a blank file. 2 Enter the following code on the page:
A ColdFusion Page Hello world, this is a ColdFusion page.
Today’s date is #DateFormat(Now())# Saving your ColdFusion page In order for the ColdFusion server to process the page, you must save the ColdFusion page on a computer where the ColdFusion MX is installed.
-
The following figure shows the cfpage.cfm in the browser: 2 Do the following tasks: a View the source code that was returned to the browser. In most browsers, you can view the source by right-clicking on page then selecting View Source. b Compare the browser source code with the source code that appears in your editor. Notice that the CFML tags were processed on the page but did not appear in the source that was returned to your browser.
-
Most often the end tag encloses the tag name in brackets and includes a slash (/), like this: The information processed by ColdFusion is placed between the start and end tag, like this: info to be processed ... ColdFusion tags, for the most part, share these common characteristics: • All start with cf. • A start and end tag. • Use of attributes (like html tags), and most attributes have values. Some ColdFusion tags, such as cfset, omit the closing tag.
-
The DollarFormat function returns a value as a string and formats that value with two decimal places, thousand separator, and dollar sign. The pounds signs (#) around the function instruct ColdFusion to evaluate the content between the pound signs and display the value. Functions and parentheses All functions have parentheses, regardless of whether the function acts on data. Consider the following function: #Now()# If you put anything inside the parentheses of the Now() function, an error would occur.
-
If you did not include the pound signs around the DateFormat(Now(), "mm/ddyyy") function, ColdFusion would not evaluate the function and the previous example would display your source code, as follows: For more information about how to use pound signs with functions, see Developing ColdFusion MX Applications. Variables Variables let you store data in memory on the server. Variables always have a name and a value.
-
In the following examples, the variables are assigned a string literal value. All string literal values are surrounded by double quotation marks. In the next example, ColdFusion uses the values of the my_first_name and my_last_name variables to set the value for the my_full_name variable in the last line of code.
-
Displaying variable output Output is what remains after the ColdFusion server processes the CFML tags on a page. Usually the output has two parts: • Information that the user sees (for example, a confirmation message) • Information that is stored by the server as a result of processing (for example, user input collected from a form) One of the tags that ColdFusion provides to display output is the cfoutput tag.
-
Building expressions In ColdFusion, you build expressions as you need them. The expressions can include simple elements, such as the expressions shown previously, or they can include complex elements, such as arithmetic functions, strings, and decision operators. (You build some complex expressions in Part II of this book.) As mentioned, it is important that elements are identified properly in your expression so ColdFusion processes them as expected, and you can avoid unnecessary errors.
-
Specifying quotation marks around values When assigning literal values to variables, you must surround the literal value with double quotation marks or single quotation marks. ColdFusion interprets the content between the quotation marks as a literal value and assigns that value to the variable; for example: ColdFusion instantiates the variable my_first_name to the string literal Kaleigh.
-
Nonsupported Equivalent ColdFusion logical operator decision operator Description >= GTE, GREATER THAN OR EQUAL TO Tests for greater than or equal to <> IS NOT, NEQ, NOT EQUAL Tests for nonequality. CONTAINS Tests whether a value is contained within a second value. DOES NOT CONTAIN Tests whether a value is not contained within a second value.
-
You use conditional processing to customize the behavior of your application. Conditional processing facilitates decision making and lets you control how the code on a page is processed. In ColdFusion, you implement conditional processing with flow control tags. These tags are similar to other programming language control elements, such as if, then, and else. Conditional processing tags ColdFusion provides several tags that let you control how a page is processed.
-
The output of this cfif statement is based on the value entered by the user. If the user enters MA in the state form field, the state tax results returned is 8.5%. If the user enters VA in the state form field, the state tax results returned is 8.2%. If the user enters any other state in the state form field, State Tax Unknown is returned. Processing form data Virtually all web applications that gather and write information to a database use a form to accomplish that task.
-
In order for the form page to find its corresponding action page, the action statement in the form tag must be correct. The form tag includes the information that tells the server where to send the data that it collects. It also tells the server how to send it. To processes these instructions to the server, the form tag uses the following syntax:
-
CHAPTER 3 Database Fundamentals This chapter provides a quick overview of relational database concepts and terms. It describes what a database is and how it is organized. It also discusses the Structured Query Language (SQL) that you use to interact with databases. Contents Understanding database basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 About SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
Understanding database basics Even though you do not need a thorough understanding of database management systems to create ColdFusion applications, you must understand some basic concepts and techniques about databases. The information in this chapter will get you started with ColdFusion. What is a relational database? A relational database is a structured collection of information that is related to a particular subject or purpose, such as an inventory database or a human resources database.
-
Understanding relational tables In a database, you can organize data in multiple tables.
-
About SQL SQL (Structured Query Language) is a language that lets you communicate with databases. For example, you can use SQL to retrieve data from a database, add data to a database, delete or update records in a database, change columns in multiple rows, add columns to tables, and add and delete tables. Using SQL to interact with a database Unlike other computer languages, SQL is made up of a small number of language elements that let you interact efficiently with a database.
-
Writing SQL and CFML statements to interact with a data source After ColdFusion makes a connection to the data source, you can interact with that database by using SQL and ColdFusion. To interact with an established data source, you need to include SQL statements in your CFML statements; for example: SELECT FirstName, LastName, DepartmentID From Employee In the previous example, the first attribute of cfquery is the name of the query.
-
Chapter 3: Database Fundamentals
-
CHAPTER 4 Configuring Your Development Environment This chapter describes how to set up your development environment for the tutorial in Part II of this book. It specifies the tutorial file structure, and how to configure the database connection and debugging options in the ColdFusion MX Administrator. Additionally, it provides a brief overview of using Macromedia Dreamweaver MX or Macromedia HomeSite+ for ColdFusion development. Contents Verifying the tutorial file structure . . . . . . . . . . . . . . .
-
Verifying the tutorial file structure Before you begin the tutorial, verify that the configuration of the computer where ColdFusion is installed matches the file structure described in the following sections. The files required to complete the Compass Travel tutorial (in Part II of this book) are installed under the web server root directory.
-
Configuring database connection and debugging options Prior to ColdFusion development, use the ColdFusion MX Administrator to define the connection to the sample database file and any optional debugging options. To access the ColdFusion Administrator, do either of the following: • Select Start > Programs > Macromedia ColdFusion MX > ColdFusion MX Administrator.
-
4 Specify the following: Field Action Database File text box Specify the location of the CompassTravel.mdb file. Click Browse to locate and select the CompassTravel.mdb file. By default, ColdFusion MX installs the CompassTravel.
-
4 Specify the following: Field Action JDBC URL Enter the following JDBC URL for the Compass Travel PointBase files: jdbc:pointbase:compasstravel,database.home=// wwwroot/cfdocs/getting_started/db The following is the default home location for stand-alone ColdFusion web server configurations: /opt/coldfusionmx/wwwroot/cfdocs/getting_started/db Driver Class Enter the following driver class: com.pointbase.jdbc.jdbcUniversalDriver Driver Name Specify PointBase. Username Specify PBPUBLIC.
-
Enabling debugging options The ColdFusion MX Administrator provides a variety of debugging settings that let you enable debugging information on a server-wide basis. If you are working on a development system, you can have these options turned on all the time. However, if you are working on a production system, you most likely will not want to have these options turned on, because the debugging information can appear on the bottom of an application page or in a dockable tree in your browser.
-
The location of the debugging information or the type of debugging data shown varies, depending on the options that you enable on the Debugging page in the ColdFusion MX Administrator. In the following example, the debugging output includes general information about the ColdFusion server, the execution time of the application, and variable information.
-
Sending debugging information to remote clients If you are using a remote client to perform the tutorial in Part II of this book, you must specify your IP address to receive debugging information. If you are working on a local client (the computer where ColdFusion is installed), this procedure is not necessary. To receive debugging information when using a remote client: 1 In the ColdFusion MX Administrator, select Debugging and Logging > Debugging IP Addresses. The Debugging IP Address page appears.
-
The Dreamweaver MX environment As a ColdFusion developer, you can build ColdFusion MX applications by writing the code manually or generating the code by using one of the code-generating tools provided with Dreamweaver MX. Features for ColdFusion developers Dreamweaver MX provides a wide variety of code editing features for ColdFusion developers, including the following: • • • • • • • • Rich tag editors for quickly setting attributes and values for every CFML tag.
-
Configuring Dreamweaver MX for ColdFusion development Before you use Dreamweaver MX to create the sample application in Part II of this book, you must configure Dreamweaver to recognize the tutorial files and data sources. To configure Dreamweaver MX to create the sample application: 1 Create a site that contains the tutorial files. 2 Specify ColdFusion as the application server document type. 3 Specify ColdFusion MX as the site application server.
-
PART II Building a ColdFusion Application Part II provides a tutorial that steps you through building a sample ColdFusion application. It consists of six lessons: Lesson 1: Preparing to Build the Sample Application . . . . . . . . . . . . . . . . . 49 Lesson 2: Writing Your First ColdFusion Application . . . . . . . . . . . . . . . . . 57 Lesson 3: Creating a Main Application Page . . . . . . . . . . . . . . . . . . . . . . . . 77 Lesson 4: Validating Data to Enforce Business Rules . . . . . . . . . . . .
-
-
CHAPTER 5 Lesson 1: Preparing to Build the Sample Application In this tutorial, you will build a simple ColdFusion web application for a fictitious travel company called Compass Travel. Compass Travel markets a wide range of adventure trips to the public through its website. Trip coordinators at Compass Travel are responsible for maintaining the trip information made available to the public.
-
Determining the application functional requirements Before you can build the sample application, you must understand the functional requirements underpinning its design. The design of the sample application centers around the daily tasks performed by Compass Travel’s trip coordinators.
-
Determining the data requirements Prior to creating the application pages to capture trip information, you must determine what type of data is required about each trip.
-
Designing the database for your application After you identify the information to collect, you must consider where to store the data. Prior to creating the data collection form and instructing ColdFusion where to store the form data, you must have a database ready to accept the data. If you had to create the Compass Travel database, you would create a table named Trips to store the information that you plan to collect about each trip.
-
Establishing a relationship between the two tables When the user selects an event type from the list obtained from reading the eventtypes table, the correct event type must be saved to the trips table with all the other trip related data. The application could store the eventType (for example, mountain climbing) itself into the eventType column in the Trips table.
-
Developing the sample application Given the application functional requirements and the database provided, you are ready to use ColdFusion to develop the Trips Maintenance application. The remaining lessons in the tutorial will step you through the process of constructing this application.
-
How to proceed Each lesson in the tutorial is designed to let you proceed at your own pace. At any time, you can stop and later return to that place in a lesson so that you can complete all the sections in the lesson. Each lesson guides you through a scenario to enhance the Compass Travel Trip Maintenance application. Sections within a lesson present basic programming concepts that you need to understand before completing the section exercise(s).
-
For more information about the tutorial file structure and the location of the getting_started subdirectories, see “Verifying the tutorial file structure” on page 38. Requirements To use this tutorial, you must have the following components installed: • ColdFusion MX For information on how to install ColdFusion MX, see Installing and Using ColdFusion MX. • • • Database Management System A database management system can be installed on the same computer as ColdFusion MX or on a separate computer.
-
CHAPTER 6 Lesson 2: Writing Your First ColdFusion Application In this lesson, you begin the construction of a ColdFusion web application for the fictitious company, Compass Travel. The exercises in this lesson guide you through the steps of creating queries and forms to search for and display trip offering information from the Compass Travel relational database. This lesson explains how to do the following tasks: • • • • Construct a query to retrieve information from a database.
-
Creating your first ColdFusion application As you recall from Lesson 1: Preparing to Build the Sample Application, two of the requirements for the Trip Maintenance application are the ability to generate trip listings and a trip query facility. You will create a search interface that meets both of these requirements in this lesson.
-
• Trip Search Results page The purpose of the Trip Search Results page is to display the results of a trip search. The primary users of these components are the Compass Travel coordinators and agents, not the general public. Application development steps You will review or participate in the following application construction steps: Steps Description 1 Create a dynamic web page that displays a list of trips. 2 Design the constraints for the search interface.
-
Using a web page to list trips To help Compass Travel agents take trip reservations by telephone and in person, the trip coordinator maintains a list of current trip offerings. Years ago, the coordinator would type the list and fax it to the various Compass Travel offices in an effort to keep everyone informed.
-
Consider a table named Clients to hold information about people with the following rows: LastName FirstName Address City Jones Tom 12 State St Boston Adams Anita 521 Beacon St Boston Green Peter 1 Broadway New York To select the columns named LastName and FirstName, use the following SELECT statement: SELECT LastName, FirstName FROM Clients The result of this SQL statement contains the following data: LastName FirstName Jones Tom Adams Anita Green Peter Using the SQL WHERE clause to
-
The result of the preceding SQL statement contains the following data: LastName FirstName Jones Tom Adams Anita You can compose a WHERE clause with one or more conditions; these are called subclauses. You join subclauses using the operators AND and OR.The AND operator displays a row if ALL conditions listed are True. The OR operator displays a row if ANY of the conditions listed are True.
-
Displaying the query result using cfoutput In Chapter 2, “CFML Basics,” on page 15, you learned that the ColdFusion cfoutput tag is an easy mechanism to display literal text and the contents of variables. Additionally, the cfoutput tag significantly simplifies displaying the results of queries. When used to display the results from a query, the cfoutput tag automatically loops through the record set for you.
-
Exercise: building a query using SQL, cfquery, and cfoutput Follow these steps to build a query that lists the current trips from the Compass Travel database. To build the query: 1 Open an editor and create a new ColdFusion page (.cfm).
-
To enhance the query results: 1 To sort the trip names in alphabetical order in the triplisting.cfm page, modify the SQL SELECT statement within the cfquery tags as follows: SELECT tripName FROM trips ORDER BY tripName 2 To display the departure, return date, and price for each trip, modify the same SQL statement.
-
Developing a search capability The dynamic listings developed in the previous exercise meet many of Compass Travel’s requirements for locating trips. However, what if the number of trips were in the thousands or tens of thousands? Locating the right trip for a customer might be difficult and certainly time consuming. Moreover, it is very hard, if not impossible, to anticipate all the ways that a user might want to search for trips.
-
Understanding search query operators Now that you decided on the queryable columns (tripLocation, departureDate, and price), you can build a simple form that allows the user to enter values for each of these fields.
-
Using SQL operators to create a search criteria page A simple design for a search criteria page presents an operator list and data entry field for each of the queryable columns. Following this pattern, a page to collect the Compass Travel Trip search criteria looks like this: Since all the code used to produce the search criteria page is HTML, you are not requested to build this page. You will, however, use this page (tripsearch.cfm) later in this lesson to test the search action page.
-
| Price | | |