问题
I'm working in a legacy app that was built upon the use of Application.cfm
files rather than Application.cfc
files.
There is a need to be able to run code after a request has processed. (Basically, I am wanting to use the <cfhtmlhead>
tag to inject some Javascript and CSS files into every loaded document. Before I was doing this with a GreaseMonkey user script, but something server-side would be best.)
From what I read, I think I should be able to do this with the onRequestEnd()
function, however, I've only ever seen that referenced in regards to Application.cfc
files. I have read that you can put an onRequestEnd.cfm
file in the same directory as an Application.cfm
file to have it register it to the onRequestEnd()
function, but the system does not map to one Application.cfm
file (i.e. I would have to throw this onRequestEnd.cfm
file in a lot of directories).
Is there some other way to register this onRequestEnd()
function using an Application.cfm
setup? In case it matters, we are running Coldfusion 9.
回答1:
Just to clarify, the onRequestEnd()
method is only available if you are utilizing the Application.cfc
file.
The OnRequestEnd.cfm
file does indeed work like the Application.cfm
file in that ColdFusion automatically looks for it and will process it's contents when found. Do note that you cannot use an OnRequestEnd.cfm page if you have an Application.cfc file for your application. So assuming that you have no Application.cfc
files for your application and are only using Application.cfm
files then the OnRequestEnd.cfm
file should work for you. All you need to do is insert the CFML code that you would like to be executed after the page request into that file.
If you have several Application.cfm
files spread out in various folders then, yes, you will also need to copy/create the OnRequestEnd.cfm
files in those directories as well. You might be able to copy stub OnRequestEnd.cfm
files in those directories that do nothing more than cfinclude
your actual code from another, single, location. At least that way once you have all of the stub files out there you can modify the code in a single place.
See the documentation for Structuring an application (it was written for ColdFusion 8 but the same rules still apply). In case that page is taken down, here is the relevant text:
How ColdFusion finds and process application definition pages
ColdFusion uses the following rules to locate and process the Application.cfc, Application.cfm, and OnRequestEnd.cfm pages that define application-specific elements. The way ColdFusion locates these files helps determine how you structure an application.
Each time ColdFusion processes a page request it does the following:
When ColdFusion starts processing the request, it does the following:
- It searches the page's directory for a file named Application.cfc. If one exists, it creates a new instance of the CFC, processes the initial events, and stops searching. (ColdFusion creates a new instance of the CFC and processes its initialization code for each request.)
- If the requested page's directory does not have an Application.cfc file, it checks the directory for an Application.cfm file. If one exists, ColdFusion logically includes the Application.cfm page at the beginning of the requested page and stops searching further.
- If the requested page's directory does not have an Application.cfc or Application.cfm file, ColdFusion searches up the directory tree and checks each directory first for an Application.cfc file and then, if one is not found, for an Application.cfm page, until it reaches the root directory (such as C:). When it finds an Application.cfc or Application.cfm file, it processes the page and stops searching.
- ColdFusion processes the requested page's contents.
- When the request ends, ColdFusion does the following:
- If you have an Application.cfc, ColdFusion processes the CFC's onRequestEnd method and releases the CFC instance.
- If you do not have an Application.cfc, but do have an Application.cfm page, ColdFusion looks for an OnRequestEnd.cfm in the same directory as the Application.cfm page ColdFusion uses for the current page. ColdFusion does not search beyond that directory, so it does not run an OnRequestEnd.cfm page that resides in another directory. Also, the OnRequestEnd.cfm page does not run if there is an error or an exception on the application page, or if the application page executes the cfabort or cfexit tag.
The following rules determine how ColdFusion processes application pages and settings:
- ColdFusion processes only one Application.cfc or Application.cfm page for each request. If a ColdFusion page has a cfinclude tag pointing to an additional ColdFusion page, ColdFusion does not search for an Application.cfc or Application.cfm page when it includes the additional page.
- If a ColdFusion page has a cfapplication tag, it first processes any Application.cfc or Application.cfm, and then processes the cfapplication tag. The tag can override the settings from the application files, including the application name and the behaviors set by the cfapplication tag attributes.
- You can have multiple Application.cfc files, Application.cfm files, and cfapplication tags that use the same application name. In this case, all pages that have the same name share the same application settings and Application scope and can set and get all the variables in this scope. ColdFusion uses the parameter settings of the cfapplication tag or the most recently processed file, if the settings, such as the session time-out, differ among the files.
Note: If your application runs on a UNIX platform, which is case-sensitive, you must spell Application.cfc, Application.cfm, and OnRequestEnd.cfm with capital letters.
回答2:
Since you are using ColdFusion 9, it would be fairly trivial to upgrade to using Application.cfc instead of trying to figure out how to plug in OnRequestEnd.cfm files. Plus, there are advantages to using Application.cfc to Application.cfm.
A few references:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_15.html
http://forum.hostek.com/showthread.php?724-Converting-to-Application-cfc
http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm
http://www.raymondcamden.com/index.cfm/2009/12/30/Best-of-CF9-Applicationcfc-Script-Template
http://cfruss.blogspot.com/2009/11/applicationcfc-reference-in-cfscript.html
来源:https://stackoverflow.com/questions/15436005/coldfusion-using-onrequestend-with-application-cfm-files