ColdFusionMX programming guide cycle in ColdFusionMX

Source: Internet
Author: User

Phase 5:Cycle in ColdFusionMX

Preface

Any program must have certain logic and algorithms, and Coldfusion is no exception. If you only rely on simple tag stacking, it is impossible to truly implement the business logic of the enterprise. In addition, I am very certain to point out that simplicity is definitely not equal to weak functionality. At present, network technology has developed into an era that focuses on performance, that is, every developed network application must implement logic while, having a rich user experience for customers is another goal. Flash + Flashremoting + cf serverside script is a highly experienced development method. For development tools, many developers have made a thorough article on the ease of use of the tools. The full uniformity of the Microsoft vs.net development platform and the wizard Method for Oracle9i JDeveloper to develop j2ee programs, macromedia DreamweaverMX supports many server-side scripting languages, which are a manifestation of ease of use of tools. What we want to explain today is the easy-to-use coldfusion loop. Many programming logics in cf can be implemented through different loop nesting in cf.

The preface also adds that the Chinese solution for operating the access database is as follows:

Http://www.flashempire.net/showthread.php? S = a85407ff13de51915c82 a57b7eb2e1ee & threadid = 125029


The first part is about Process Control and circulation in CF.

Click the cf flow label in the Insert panel of DreamweaverMX to see the following line icon:

This line of icons almost completes the most important process and loop processing in coldfusion. When talking about the process, the first thing we come into contact with when learning any language is if... Then... Else process control statement. Presumably many people who are engaged in web design will inevitably be exposed to this logic. This logic is simple, but it is the most commonly used. In, when "if else elsif" is displayed in text, the if process control in cf is used. When you click "if", we observe the page using the original code, and you will see that the <cfif> </cfif> code segment has been inserted at the cursor. We use <cfxxx>... </Cfxxx> it is regarded as a closed code segment, no matter how long it is or how short it is.

The standard simple description of cfif labels should be as follows:

<Cfif the condition you want to determine>

What you need to do: it can be a line of output or several other complex code segments.

</Cfif>

Therefore, we often use some operators to determine the direction of the cfif process, because in the logical judgment of cfif, there are only true (1) and false (0, if the condition is true, the nested transactions in the cfif tag will be executed. If the condition is false, the logic will jump out and process the next logic. Then the operator in cf is very human. It uses language to express and judge, rather than sign. For example, "=" must be expressed as "EQ" in the condition operator of cfif, and ">" must be expressed as "greater than" or "GT ". If we determine whether a is equal to B, we should write in cf as follows:

<Cfif a eq B>

<Cfset A = B>

</Cfif>

For detailed condition operators, see the following table:

Operator)

Abbreviations

Operator)

Abbreviations

IS (=)

EQUAL, EQ

Great than or equal to (> =)

GTE, GE

Is not (<>)

Not equal, NEQ

Less than or equal to (<=)

LTE, LE

Greater than (>)

GT

Contains (including)

Less than (<)

LT

Does Not Contain (Not included)

The other else and elsif are nested in the complete cfif. The following code provides a complete description:

<Cfif expression 1>

Code segment 1

<Cfelseif expression 2>

Code Segment 2

<Cfelse>

Code segment 3

</Cfif>

If expression 1 is true, code segment 1 is used. If expression 1 is not true, expression 2 is used to determine whether expression 2 is true. If expression 2 is true, code segment 2 is used; otherwise, code segment 3 is used. If you do not have any basic development skills, you can try this little code and then run it to understand the result.

<Cfif 1 is 0>

<Cfoutput> 1 </cfoutput>

<Cfelseif 2 is 0>

<Cfoutput> 2 </cfoutput>

<Cfelse>

<Cfoutput> 3 </cfoutput>

</Cfif>

In the process of controlling the process, we also use the switch... The case method is also supported in coldfusion programming. For the Switch... The case method is also very easy to use. Here is a simple explanation. Click the three icons to complete the cfswitch operation. The purpose is to allow developers to select a process under the same expression with multiple result values. After clicking the icon on the left, we can see the following window:

The syntax is as follows:

<Cfswitch expression = "# var #">

<Cfcase value = "x1">

Action1

</Cfcase>

<Cfcase value = "x2">

Action2

</Cfcase>

...

<Cfdefaultcase>

Default action

</Cfdefaultcase>

</Cfswitch>

Syntax interpretation is very simple. For expression expressions, the value attribute of cfcase is used to reflect the value of # var # In the expression, and then different values are used to respond to different actions, perform different operations. If no value matches, perform the default action in the cfdefaultcase label. Take a look at the official macromedia standard program section:

<Cfquery name = "GetEmployees" dataSource = "cfsnippets">

SELECT Emp_ID, FirstName, LastName, EMail, Phone, Department

FROM Employees

</Cfquery>

<H3> cfswitch Example

<P> Each time the case is fulfilled, the specific information is printed;

If the case is not fulfilled, the default case is output </p>

<Cfoutput query = "GetEmployees">

<Cfswitch expression = "# Trim (Department) #">

<Cfcase value = "Sales">

# FirstName # LastName # is in <B> sales </B> <br>

</Cfcase>

<Cfcase value = "Accounting">

# FirstName # LastName # is in <B> accounting </B> <br>

</Cfcase> <cfcase value = "Administration">

# FirstName # LastName # is in <B> administration </B> <br>

</Cfcase>

<Cfdefaultcase>

# FirstName # LastName # is not in Sales, Accounting, or

Administration. <br>

</Cfdefaultcase>

</Cfswitch>

</Cfoutput>

This Code uses the cfswitch to display the Employees in the Employees table of the cfmx default data source cfsnippets, and uses the department variable to differentiate the cfcase values in the cfswitch.

Let's look at the other three Icon Sets in the cf flow panel: The three icons are cftry, cfcatch, and cfthrow from left to right. These three tags play an important role in compiling and debugging cf applications. These three labels are used to handle application errors and exceptions. Why should we handle errors and exceptions? I think there are two major roles for the author. The first is to remind developers of application errors or exceptions. At the same time, when the system encounters errors or exceptions after being delivered to the customer, it can be processed in different ways to give the customer a reliable feeling. Second, reduce the possibility of application attacks. If errors and exception handling are not used, we can often see that if an error occurs during program processing, information that should not appear will be exposed in the web browser, such as the database table name and field name, even database operations. In fact, these error messages should not be displayed in the web browser.

When writing a coldfusion application, we should pay attention to the possible error types, so that we can use the settings in the cfmx administrator and the cftry tag to handle these errors. Common Errors include syntax errors, database operation errors, application server environment errors, logical errors, and verification errors. Sort by the importance of these errors. The first error is the application server environment error. These errors are generated by the application server, which may cause the entire coldfusion application to fail to run. Therefore, it is the most important position. For other errors, we should try to reduce syntax and logic errors because such errors can be corrected through continuous debugging, and debugging of such errors consumes more energy.

Let's take a look at this. This is one of my operations on cfmx administrator-> debugging settiongs:

If you select the Enable Robust Exception Information option, when an error occurs on the cfm page, you can view the application error Information displayed in the browser, including the physical path of the page template and the URI of the page, the number of wrong rows (not necessarily accurate), the database statements for operations, the data source name, and the trace method of the Java stack. In this case, some information is obviously not displayed after you submit the project to the customer. Therefore, after installing coldfusionMX on the production server, you should check this option.

After a brief introduction of <cftry> and <cfcatch>, many people may not use it. Therefore, we use the simplest database operation to describe the role of error handling. To recall the previous program, we will first write down a simple database query section:

<Cfquery name = "demo" DATASOURCE = "cfsnippets">

SELECT Emp_ID, LastName

FROM Employees

</CFQUERY>

If we change the cfsnippets name to xxx, what will happen? See the following:

The above is the error message display page automatically provided by cf server. On this page, let's see what important information we provide to external visitors? The data source name, the physical path of the file, and the SQL query statement. This information gives malicious visitors full access. We should use <cftry> and <cfcatch> to avoid displaying these error messages. The modified code is as follows:

<Cftry>

<Cfquery name = "demo" DATASOURCE = "xxx">

SELECT Emp_ID, LastName

FROM Employees

</CFQUERY>

<CFCatch Type = "database">

Sorry! We cannot connect to the database server! Sorry!

<CFAbort>

</CFCatch>

</Cftry>

The following information is displayed in the browser:

"Sorry! We cannot connect to the database server! Sorry !"

In this way, many very important information related to security is protected, which also reduces the possibility of malicious attacks on the server.
The second part goes deeper on the basis of the original

After learning the basic coldfusionMX knowledge in five parts, many developers can write some simple applications. Then, let's go deeper on the original basis. This section describes the latest CFComponent basics of coldfusionMX. The CFComponent information in this tutorial is from the official website of macromedia.

Before you start to explain CFComponent (CFC), let's take a brief introduction to CFC. Many developers may be familiar with asp. asp can be used in combination with com and com +, while com is a component technology based on Microsoft. I believe that the component development technology is a set of highly available code for applications. What is availability? In English, it is reuse. The major aspect is code reuse. CFC is no exception. CFC is formed by simply combining multiple cfml code segments with different functions and assigning different access methods. So what are the advantages and differences between CFC and custom CF tags? The biggest advantage of CFC is that it does not contain code that represents the nature. That is to say, a CFC has some pure logic, different call methods and additional modifier labels are used to present different external results. They fully meet the needs of web technology development, that is, performance and logical separation. In contrast, the custom tag is not like this. It constructs a Function that can mix the performance with the logic and present the results through operations on other cfm pages. Another difference is that CFC is an object with different access methods, method entry operations, and parameter control methods. If the above explanation is hard to understand, we will use a frequently-developed module to illustrate the problem. We often develop user registration and verification modules when developing interactive websites. According to the general development method, there are many pages, such as login. cfm, checkuser. cfm and so on. Our logic code will be written in all the background files to be processed. What kind of troubles will happen? You can use <cfquery> to construct different database operation code segments to search for user names and passwords, return the search results, and verify the input values of form and database results, different embedded logic code on different pages can be used to perform different operations on users. This development method is not impossible, but we recommend that cf developers use a more advanced development method CFC to develop corresponding functions. So what is the development idea of CFC with the same function module? We can aggregate the required code segments and convert them into the unique Components through a unique CFC Tag. In addition, we can assign them different methods. For the functional modules of user registration and login verification, we can switch to this idea:

L define a user object and encapsulate all common operations on the user in this object

L defines operation methods for user objects, such as add (), delete (), update (), get (), and other general operation methods. You can also add some more, for example, verifyPassword () and GetEmail.

L pass different parameters to specific methods for different users to implement corresponding functions.

Well, you can tell the advantages of using CFC: high availability, high development efficiency, and high scalability.

So what can we use to construct a CFC? Complicated? A headache? None of them. You can use basic cfml syntax knowledge, plus several tags for constructing CFC. Several tags that construct CFC are as follows:

L <CFComponent> define a CFC

L <CFFunction> defines the method in a CFC)

L <CFArgument> defines the parameters received by the method.

L <CFReturn> Returns a value or method.

Let's construct a very simple cfc. Let's look at the following code:

<!--- Browser id component --->
<CFCOMPONENT>
    <!--- Is the browser IE? --->
    <CFFUNCTION NAME="IsIE">
                               <!--- Init variable --->
                               <CFSET result="No">
                               <!--- Look for IE identifier --->
                               <CFIF FindNoCase("MSIE", CGI.HTTP_USER_AGENT)>
                                              <!--- Yep, got it --->
                                              <CFSET result="Yes">
                               </CFIF>
                               <!--- Return result --->
               <CFRETURN result>
               </CFFUNCTION>
</CFCOMPONENT>

File Storage becomes browser. cfc. We can see that all the cf code is between the <cfcomponent> </cfcomponent> labels. The cfc has a Function called IsIE to determine the type of the browser. The default result is No. If MSIE is detected (case-insensitive), the result is true. The value returned by this Function is result. It's easy, isn't it?

After constructing the Component browser. cfc, how can we apply this browser. cfc? Generate a test. cfm file in the directory where browser. cfc is stored. The code in this file is written as follows:

<!--- Invoke browser CFC --->
<CFINVOKE COMPONENT="browser"
METHOD="IsIE"
RETURNVARIABLE="result_ie">

<!--- Feedback --->
<CFOUTPUT>
Your browser is:<BR>
IE: #YesNoFormat(result_ie)#<BR>
</CFOUTPUT>

Run the test. cfm file and the result is:

Your browser is:
IE: Yes

The file Test. cfm uses the <cfInvoke> label to call the cfc of browser and the IsIE method. The returned value is result_ie. The above cfc is just a program for judging ie browsers. The following is a complete one:

<CFCOMPONENT>

<! --- Is the browser IE? --->

<Cffunction name = "IsIE"

RETURNTYPE = "boolean"

HINT = "Is browser Microsoft IE">

<! --- If no browser id passed, used current --->

<Cfargument name = "browser"

REQUIRED = "no"

DEFAULT = "# CGI. HTTP_USER_AGENT #"

HINT = "Browser ID, defaults to cgi id">

<! --- Init variable --->

<CFSET result = "No">

<! --- Look for IE identifier --->

<CFIF FindNoCase ("MSIE", browser)>

<! --- Yep, got it --->

<CFSET result = "Yes">

</CFIF>

<! --- Return result --->

<CFRETURN result>

</CFFUNCTION>

<! --- Is the browser Netscape? --->

<Cffunction name = "IsNetscape"

RETURNTYPE = "boolean"

HINT = "Is browser Netscape">

<! --- If no browser id passed, used current --->

<Cfargument name = "browser"

REQUIRED = "no"

DEFAULT = "# CGI. HTTP_USER_AGENT #"

HINT = "Browser ID, defaults to cgi id">

<! --- Init variable --->

<CFSET result = "No">

<! --- Look for Netscape identifier and no IE identifier --->

<CFIF FindNoCase ("mozilla", browser) and not FindNoCase ("MSIE", browser)>

<! --- Yep, got it --->

<CFSET result = "Yes">

</CFIF>

<! --- Return result --->

<CFRETURN result>

</CFFUNCTION>

<! --- Is the browser Dreamweaver? --->

<Cffunction name = "IsDreamweaver"

RETURNTYPE = "boolean"

HINT = "Is browser Dreamweaver">

<! --- If no browser id passed, used current --->

<Cfargument name = "browser"

REQUIRED = "no"

DEFAULT = "# CGI. HTTP_USER_AGENT #"

HINT = "Browser ID, defaults to cgi id">

<! --- Init variable --->

<CFSET result = "No">

<! --- Look for DW identifier --->

<CFIF FindNoCase ("mmhttp", browser)>

<! --- Yep, got it --->

<CFSET result = "Yes">

</CFIF>

<! --- Return result --->

<CFRETURN result>

</CFFUNCTION>

<! --- Identify a browser

Returns: IE-Internet Explorer

NS? Netscape

DW? Dreamweaver

Empty string is unknown

--->

<Cffunction name = "Identify"

RETURNTYPE = "string"

HINT = "Identify a browser">

<! --- If no browser id passed, used current --->

<Cfargument name = "browser"

REQUIRED = "no"

DEFAULT = "# CGI. HTTP_USER_AGENT #"

HINT = "Browser ID, defaults to cgi id">

<! --- Init variable --->

<CFSET result = "">

<CFIF IsIE (browser)>

<CFSET result = "IE">

<CFELSEIF IsNetscape (browser)>

<CFSET result = "NS">

<CFELSEIF IsDreamweaver (browser)>

<CFSET result = "DW">

</CFIF>

<! --- Return result --->

<CFRETURN result>

</CFFUNCTION>

</CFCOMPONENT>

The 2nd CFC above is a bit more complicated. We overwrite the storage into browser. cfc, which contains four functions (methods ):

IsIE: whether the browser is MS Browser; IsNescape: whether it is Nescape (mozilla) browser; IsDreamweaver: whether it is embedded with Dreamweaver; Identify: returns all results. If none of the three are, an empty string is returned. The program also adds two new properties for CFFunction: ReturnType is a type validation (validate) of the returned value. If it is not the type described by ReturnType, an error is displayed. In addition, the <CFArgument> label is added to the Function to set the BrowserID value and set the Required attribute to no to ensure that browserid cannot be detected, provides a default cgi id. If the required attribute is set to yes, an error is returned if the specified parameter is passed incorrectly. We can use the test. cfm program below to call the preceding complicated browser. cfc:

<! --- Check for IE --->

<Cfinvoke component = "browser"

METHOD = "IsIE"

RETURNVARIABLE = "result_ie">

<! --- Check for Netscape --->

<Cfinvoke component = "browser"

METHOD = "IsNetscape"

RETURNVARIABLE = "result_ns">

<! --- Check for DW --->

<Cfinvoke component = "browser"

METHOD = "IsDreamweaver"

RETURNVARIABLE = "result_dw">

<! --- Identify browser --->

<Cfinvoke component = "browser"

METHOD = "Identify"

RETURNVARIABLE = "result_id">

<! --- Feedback --->

<CFOUTPUT>

Your browser is: <BR>

IE: # YesNoFormat (result_ie) # <BR>

NS: # YesNoFormat (result_ns) # <BR>

DW: # YesNoFormat (result_dw) # <BR>

ID: # result_id # <BR>

</CFOUTPUT>

The result is as follows:

Your browser is:
IE: Yes
NS: No
DW: No
ID: IE

In fact, we can observe a cfc from the inside in two ways. The first is to access the cfc through a direct url. The url of the above 2nd final browser. cfc on my local machine is:

Http: /localhost: 8500/cfdev/browser. cfc

After entering this url, you will first see the cf administrator interface. after entering the login password, the system will report a detailed information about browser. cfc to the developer, as shown below:

The cfc structure and details are described in detail. To see this, you must have the password permission for the cf server administrator!

The second method is to import the cfc through dwmx, so that dwmx can automatically identify the CFC:

Then we can directly call the cfc method from the component panel to drap & drop it to the programming area on the right.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.