简体   繁体   中英

How to handle a request in ColdFusion

I am a newbie to ColdFusion, I created a result.cfm page, now I just want to do some actions in Application.cfc when the user calls /result.cfm . Something like below:

 <cfif 'if the request is for result.cfm'> <!-- do some action --> </cfif> 

Is there any way to handle the request?

<cfif REFindNoCase("^/result.cfm", CGI.SCRIPT_NAME)>
  <!--- do some action --->
</cfif>

or if you want to match more pages and subfolders you could do:

<cfset patterns = [
    "^/foo/",
    "^/bar/",
    "^/etc/",
    "^/login.cfm",
    "^/baz.cfm"
] />

<cfif REFindNoCase("(" & ArrayToList(patterns, ")|(") & ")", CGI.SCRIPT_NAME)>
    <!--- do some action --->
</cfif>

A CFC is what Coldfusion calls a component, but it's essentially an object with methods. When any page on your site is requested coldfusion sends the name of your page to the onRequest method of your Application.cfc. By default that method looks something like this...

<cffunction name="OnRequest" access="public" returntype="void" output="true">

    <cfargument name="TargetPage" type="string" required="true" />

    <cfinclude template = "#arguments.targetPage#" />

</cffunction>

The TargetPage is the relative path to the page that has been requested.

I'm not sure what you're trying to do do, but you can either just create the page result.cfm and do your coding in there, or if you need to you can intercept the call by creating your own onRequest method and putting it in your application.cfc

<cffunction name="OnRequest" access="public" returntype="void" output="true">

    <cfargument name="TargetPage" type="string" required="true" />

    <cfif arguments.targetPage is "requestresult.cfm">
        <!--- Do something else --->
    <cfelse>
        <cfinclude template = "#arguments.targetPage#" />
    </cfif>

</cffunction>

Note that onRequest isn't the only method in Application.cfc, so that's worth looking up. Also note you might want to output arguments.targetPage when you try this to just double check if the slash comes with the request or not (can't remember)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM