Fifth issue: Loops in the Coldfusionmx
Preface
Any kind of procedure is simpler also must have certain logic and the algorithm, ColdFusion is no exception. If just rely on a simple tag stack, it is impossible to truly achieve business logic. Moreover, I am very certain here that the simple is not equal to the weak function. At present, the network technology has developed to a performance-oriented era, is the development of each of the network applications, in order to achieve the logic at the same time, with the customer has a rich user experience is another pursuit of the goal. FLASH+FLASHREMOTING+CF serverside script is a very experienced development tool. And for development tools, many developers are more in the use of tools to do the article, Microsoft Vs.net Development Platform Integrity, oracle9i JDeveloper Development Java Program Wizard Way, Macromedia Dreamweavermx the support of numerous server-side scripting languages is a manifestation of the ease of use of tools. What we are going to explain to you today is the simple and easy-to-use ColdFusion loop. Many of the logic that can be programmed in CF is achieved through different loops nesting in cf.
Here's the preface to add a point to the operation of the Access database in the Chinese solution, if any developer interested, please go here to see and participate in the discussion:
http://www.flashempire.net/showthread.php?s=a85407ff13de51915c82a57b7eb2e1ee&threadid=125029
The first part looks at the process control and loop in CF
When we click on the CF Flow tab in the Insert panel in DREAMWEAVERMX, we see a line of icons as follows:
This line of icons almost completes the most important process and loop processing in the ColdFusion. When it comes to the process, the first thing we contact in the process of learning any language is IF...THEN...ELSE this process control statement. Presumably many friends engaged in web design are inevitably exposed to this logic. This logic is simple, but it is the most commonly used. In the above illustration, displaying "if Else elsif" in text is controlled using the IF process in cf. When you click on the text "If", we look at the page in the same way as the original code, and we see that the <cfif></cfif> code snippet has been inserted at the cursor. We think of all the <cfxxx>...</cfxxx> in CF as a closed code snippet, no matter how long it is or how short it is.
Using the CFIF tag, a simple description of the standard should be:
<cfif you have to judge the conditions >
What you want to do: it can be one line of output, or it can be a number of other complex pieces of code.
</cfif>
Then we often use some of the operators of the conditions to determine the direction of the CFIF process, because in CFIF's logical judgment, only true (1) and False (0) two, if the condition is true, do cfif tags in the middle of the nested transaction, if False, jump out of this logic, to deal with the next logic. So the operator in CF is very human, and it uses language to express judgment, not symbols. For example, "=" is expressed as "EQ" in the CFIF condition operator, and ">" is expressed as "GREATER THAN" or "GT". If we determine whether a is equal to B, then in CF we write this:
<cfif A EQ b>
<cfset a=b>
</cfif>
Use the detailed conditional operator to see the following table:
Operator (interpretation) |
Abbreviation |
Operator (interpretation) |
Abbreviation |
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 (excluding) |
|
Then other else and elsif are nested in the full CFIF, and the following code is a complete description:
<cfif expression 1>
Code Snippet 1
<cfelseif expression 2>
Code Snippet 2
<cfelse>
Code Snippet 3
</cfif>
If the expression is set up, then do code paragraph 1, if the expression 1 does not set up, then judge the expression 2 is set up, if the expression 2 is established, then do code paragraph 2, otherwise do code segment 3. You do not have the foundation of the development of friends, you can try this small code, then run to understand the results.
<cfif 1 is 0>
<cfoutput>1</cfoutput>
<cfelseif 2 is 0>
<cfoutput>2</cfoutput>
<cfelse>
<cfoutput>3</cfoutput>
</cfif>
Development program in the process of controlling the process, we also use the Switch...case method, in the ColdFusion programming method is also supported. And for the Switch...case method, it is also very simple to use, here for a brief explanation. Click on the three icons to complete the cfswitch operation, with the intention of allowing the developer to choose the same expression for a variety of result values under the conditions of the process. After clicking on the icon on the left, we will see the following window:
The syntax operates as follows:
<cfswitch expression= "#var #" >
<cfcase value= "x1" >
Action1
</cfcase>
<cfcase value= "X2" >
Action2
</cfcase>
...
<cfdefaultcase>
Default action
</cfdefaultcase>
</cfswitch>
The syntax explanation is very simple, for the expression expression, uses the Cfcase value attribute to reflect the #var# value in the expression, then responds to the different action for the different value, does the different operation, if does not have the value match, Then do the default action in the Cfdefaultcase label. Take a look at the Official Macromedia Program section:
<cfquery name = "GetEmployees" DataSource = "Cfsnippets" >
SELECT emp_id, FirstName, LastName, EMail, Phone, Department
From Employees
</cfquery>
<p>each time the "case is fulfilled", the specific information is printed;
If the case isn't 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><br>
</cfcase>
<cfcase value= "Accounting" >
#FirstName # #LastName # is in <b>accounting</b><br><br>
</cfcase> <cfcase value= "Administration" >
#FirstName # #LastName # is in <b>administration</b><br><br>
</cfcase>
<cfdefaultcase>
#FirstName # #LastName # isn't in Sales, Accounting, or
Administration.<br><br>
</cfdefaultcase>
</cfswitch>
</cfoutput>
This code is passed cfswitch to display the people in the Employees table in the CFMX default data source Cfsnippets, and the way is distinguished by the different values department in Cfswitch (where the department is located).
Let's look at a collection of 3 other icons in the CF flow panel: The 3 icons are left to right, followed by Cftry, Cfcatch, and Cfthrow. These 3 tags play an important role in the process of writing and debugging CF applications. These 3 tags are actually handling the errors and exceptions of the application. Why should there be error and exception handling? The author believes that there are two major aspects of the role. The first is to remind developers of an error or exception to the application, and to make it possible to use different methods of processing to give the customer a sense of reliability after the system is delivered to the customer with errors or exceptions. The second is to reduce the likelihood of an application being compromised. If we do not use error and exception handling, then we often see that if the program processing errors, the Web browser will expose the information should not appear, such as database table name, field name, even the operation of the database and so on. In fact, these error messages should not be displayed externally in a Web browser.
When writing a ColdFusion application, we should be aware of the possible types of errors that can be used to handle these errors using the CFMX administrator's settings and the cftry tag combination. The errors that we often encounter fall into five categories: syntax errors, database operation errors, application server (application Server) environment errors, logical errors, and validation errors. Sorted by the importance of these errors, the first is the application server environment error, which is generated by Application server, which may cause the entire ColdFusion application to not run. So it's in the most important position. The rest of us should try to reduce syntax and logic errors, because this error can be fixed by constant debug, and the debugging of such errors consumes more energy.
Let's take a look at the diagram below, which is a screenshot of my operation of CFMX administrator->debugging settiongs:
If you choose Enable robust Exception information This option, when the CFM page is wrong, you will see the application error message displayed in application through the browser, including the physical path of the page template, the URI of the page, The number of error rows (not necessarily accurate), the database statement of the operation, the data source name, the trace method of the Java stack, and so on. Some of this information is obviously that you do not want to display after the project is submitted to the customer, so after installing COLDFUSIONMX on production server, remove this option.
With a simple introduction to <cftry> and <cfcatch>, perhaps a lot of people will not use, then we use a very simple database operation to illustrate the role of error handling. To recall the previous program, we first write down a simple section of the database query program:
<cfquery name= "Demo" datasource= "Cfsnippets" >
SELECT emp_id, LastName
From Employees
</CFQUERY>
If we change the name of cfsnippets to XXX, then we'll see what happens? Take a look at the screenshot below:
This screenshot above is the error message Display page that is automatically given by CF server. On this page, see what important information we provide to people from outside the audience? The data source name, the file's physical path, and the SQL query's statement. This information is entirely for those with malicious visitors to exploit. Then we should use <cftry> and <cfcatch> to avoid the display of these error messages. The following code is modified:
<cftry>
<cfquery name= "demo" datasource= "xxx" >
SELECT emp_id, LastName
From Employees
</CFQUERY>
<cfcatch type= "Database" >
I am sorry! We can't connect to the database server! I'm so sorry!
<CFAbort>
</CFCatch>
</cftry>
The display information in the browser that we see later is:
Sorry We can't connect to the database server! I'm so sorry! ”
In this way, many very important, security-related information is protected, as well as reducing the likelihood that the server will be maliciously attacked.
The second part of the original based on the in-depth
Many developers have been able to do some simple application writing after learning about the basic coldfusionmx of five parts. So we are on the basis of the original, in-depth. Explain the basics of Coldfusionmx's latest cfcomponent. Some of the cfcomponent in this tutorial are derived from Macromedia officials.
Before beginning to explain the Cfcomponent (hereinafter referred to as CFC), a brief summary will be made to allow developers to introduce the CFC as a whole. Many developers may be familiar with ASP, ASP can be combined with com,com+ application and COM is based on a Microsoft component technology. To say that the technology is advanced or easy to use, the component development technology is what I see as a set of highly available code for the application. What is availability? In English is reuse. A professional point is code reuse. CFC is no exception, through a simple combination of many different functions of the CFML Code section, and given different access methods, constitutes a CFC. So what are the advantages and differences in CFC's custom labels for previous CF? The biggest advantage of CFC is that it does not contain the performance of the code, that is, a CFC has some kind of pure logic, and then through different calls and additional modifiers to render the external results, is fully consistent with the requirements of web technology development, that is, performance and logic separation. And, custom tag is not so, it is a function, you can mix performance and logic, through the operation of other CFM pages to render the results. There is another difference is that CFC is an object, has different access mode, also has the method of entry operation, but also has a parameter control mode. If the above explanation is difficult to understand, then we use a frequently developed module to illustrate the problem. We often develop interactive web sites, often develop user registration and validation module, according to the general development method, there will be many pages, such as login.cfm, checkuser.cfm, and so on a bunch of pages for user login (registration) operation. Our logical code is written in all the background files that need to be processed. So what kind of tumultuous phenomenon will occur? Using <cfquery> to make a different database operation code section to find the user name and password, return the search results, the form form input values and database results validation, through different pages of different embedded logic code to the corresponding user's different operations. This development is not possible, but we recommend that the CF developer adopt a more advanced development method CFC to develop the corresponding function. So the development of the same functional modules of CFC is what kind of development ideas? We can assemble the code snippets that are needed to make these snippets unique components by the special tag that makes up the CFC, and give them different method. Or for user registration and Login Verification function module, we replaced this idea:
L defines a user object that encapsulates all of the common operations for users in this object
L Define the action methods for user object, such as Add (), delete (), update (), get (), and so on, some general methods of operation, also can add some, such as VerifyPassword (), Getemail () and so on.
L for different users to pass different parameters to specific methods to achieve the corresponding function.
Okay, I don't need to tell you, you can already identify the advantages of using CFCs: strong usability, high development efficiency, and strong scalability.
So, what do we use to construct a CFC? It's complicated? A headache? are not, with the basic CFML grammar knowledge, plus a few specific construction of the CFC's tag can be achieved. Then the construction of the CFC several tag is as follows:
L <CFComponent> Define a CFC
L <CFFunction> Define a method of operation in a CFC
L <CFArgument> Define parameters received by method
L <CFReturn> Returns a value, or returns from method.
We now construct a very simple CFC, looking 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, and all of us can see that all CF codes are between <cfcomponent></cfcomponent> tags. This CFC has a function called Isie, which is designed to determine the type of browser. The default result is no, so if you detect a MSIE string (case-insensitive), the result is true. The value returned by this function is result. It's simple, isn't it?
After constructing BROWSER.CFC this component, how to apply this BROWSER.CFC? Regenerate a test.cfm file in the directory where the BROWSER.CFC is stored, and the code in this file is written in the following way:
<!--- Invoke browser CFC --->
<CFINVOKE COMPONENT="browser"
METHOD="IsIE"
RETURNVARIABLE="result_ie">
<!--- Feedback --->
<CFOUTPUT>
Your browser is:<BR>
IE: #YesNoFormat(result_ie)#<BR>
</CFOUTPUT>
After executing test.cfm This file, the result is:
Your Browser is:
Ie:yes
Test.cfm This file uses the <cfInvoke> tag to invoke the browser this CFC, and calls Isie this method, the return value is the use of Result_ie. The above CFC is just a program to determine IE browser, the following is a full:
<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 complex, and we cover storage as BROWSER.CFC, and this CFC contains 4 function (method):
Isie, test whether the browser is a MS browser, Isnescape test is Nescape (Mozilla) browser, Isdreamweaver test whether the embedded dreamweaver;identify return all the results, if the above 3 kinds are not , and returns an empty string. The program also added two new properties for Cffunction: ReturnType is a type acknowledgment (validate) of the return value, and an error will be displayed if it is not the type described by ReturnType. In addition, our function also added <CFArgument> this tag to set the value of Browserid, and set the required property to no is to ensure that there is no detected Browserid, provide a default CGI ID. If the required property is set to Yes, the specific parameter pass will be an error if it does not meet the requirements. We can invoke this complex BROWSER.CFC by using the following TEST.CFM program:
<!---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 results displayed are:
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 through the direct URL access. Above the 2nd final browser.cfc in my native URL is:
Http://localhost:8500/cfdev/browser.cfc
Enter this URL, you will first see the interface of CF Administrator, enter the login password, the system will report a BROWSER.CFC detailed information to the developer, screenshot as follows:
The diagram above describes the structure and details of this CFC, and wants to see the need for a CF server administrator's password permissions.
The second method is to import the CFC through DWMX, so you can see that the DWMX will automatically identify the CFC:
Then we can directly drap the required CFC method directly from the palette of components & drop to the programming area on the right.