【问题标题】:apllication.cfc does not fire any default cffunction? Coldfusionapplication.cfc 不会触发任何默认 cffunction?冷融合
【发布时间】:2026-02-11 02:10:01
【问题描述】:

谁能告诉我问题是什么?我试图运行一些 .cfm 文件,但除了 cfcomponent 之外,它不会触发 cffunction 的任何效果?我错过了什么吗?谁能给我解释一下?

<cfcomponent>
  <cfset THIS.Name = "formdemo">
  <cfset THIS.SessionManagement = true>
  <cfset This.Sessiontimeout="#createtimespan(0,0,20,0)#">
  <cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">
  --Entered cfcomponent--
  <cffunction name="onApplicationStart" returnType="boolean" output="false">
    --Entered Application Start--
    <cfset application.portfolioUploadRoot = "ram:///portfoliouploads">
    <cfif not directoryExists(application.portfolioUploadRoot)>
      <cfdirectory action="create" directory="#application.portfolioUploadRoot#">
    </cfif>
    <cfreturn true>
  </cffunction>
  <cffunction name="onSessionStart" returnType="void" output="false">
    --Entered Session Start--
    <cfset session.myuploadroot = application.portfolioUploadRoot & "/" & replace(createUUID(), "-", "_", "all")>
    <cfif not directoryExists(session.myuploadroot)>
      <cfdirectory action="create" directory="#session.myuploadroot#">
    </cfif>
  </cffunction>
  <cffunction name="onApplicationEnd" returnType="void" output="false">
    --Entered Application End--
    <cfargument name="applicationScope" required="true">
    <cfif directoryExists(arguments.applicationScope.portfolioUploadRoot)>
      <cfdirectory action="delete" recurse="true" directory="#arguments.applicationScope.portfolioUploadRoot#">
    </cfif>
  </cffunction>
  <cffunction name="onSessionEnd" returnType="void" output="false">
    --Entered Session End--
    <cfargument name="sessionScope" type="struct" required="true">
    <cfargument name="appScope" type="struct" required="false">
    <cfif directoryExists(arguments.sessionScope.myuploadroot)>
      <cfdirectory action="delete" recurse="true" directory="#arguments.sessionScope.myuploadroot#">
    </cfif>
  </cffunction>
</cfcomponent>

结果只在cfmpage.cfm开头显示“--Entered cfcomponent--”。

cfmpage.cfm:

<cfparam name="form.textname" default="">
<cfparam name="form.textemail" default="">
<cfparam name="form.docattach" default="">
<cfif structKeyExists(form, "Submit")>
<cfset form.textname = trim(htmlEditFormat(form.textname))>
<cfset form.textemail = trim(htmlEditFormat(form.textemail))>
<cflocation url="formcomplete.cfm" addToken="false">
</cfif>
<!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body>
    <form method="post" enctype="multipart/form-data">
      <cfoutput>
        <input id="textname" name="textname" type="text" class="input-large" required="" value="#form.textname#">
        <input id="textemail" name="textemail" type="text" class="input-large" required="" value="#form.textemail#">
      </cfoutput>
    </form>
  </body>
</html>

【问题讨论】:

    标签: coldfusion default coldfusion-10 cfc cffunction


    【解决方案1】:

    它正在做它应该做的。

    onApplicationStart -- Runs when ColdFusion receives the first request for a page in the application.
        For this, to easily see this, you can try changing the name of the application, then
        visit a page within.
    
    onSessionStart -- Only run upon the first visit within the session. If you wait til after the
        timeout and then come back, you'll see this. Changing the application name will should also
        retrigger this.
    
    onSessionEnd -- Run when the session ends. It will trigger after the timeout, it's used so that
        you can clean up activity. For instance, if you're using something like Application.NumberOnline.
        OnSessionEnd can substract one (where onSessionStart) can add one.
    
    onApplicationEnd -- Runs when the application timeout occurs or the server is shutting down.
    

    后两者都不会在屏幕上显示任何文本,因为您在访问页面时看不到该事件。但是,您可以手动调用它们来触发它们的效果。但是,您可以根据自己的选择记录这些操作。例如,您可以以这种方式使用 cflog:

    <cffunction name="onApplicationEnd">
        <cfargument name="ApplicationScope" required=true/>
        <cflog file="#This.Name#" type="Information" 
            text="Application #Arguments.ApplicationScope.applicationname# Ended" >
    </cffunction>
    

    换句话说:

    <cfscript>
       ap      =   createObject("component","Application");
       ap.onSessionEnd(session,application);
    </cfscript>
    

    将显示文本,因为它正在触发事件。

    最后,如果您希望在每个页面上发生一些事情,onRequestStart 和 onRequestEnd 或 onRequest 是不错的选择。 OnRequest 是一种环绕页面的方法,因此您可以在同一请求中执行页眉和页脚操作,但您必须显式包含文件,而 onRequestStart / onRequestEnd 在请求的开始和结束处执行。

    动作方法调用的顺序是

    1. onApplicationStart(在第一个应用程序活动时运行)
    2. onSessionStart(在第一个会话活动上运行)
    3. onRequestStart
    4. onRequest
    5. onRequestEnd

    最后,函数不会触发,除非它们被调用。这也适用于您自己编写的函数。

    <cfscript>
      function foo() {
        writeoutput("bar");
      }
    </cfscript>
    

    在您真正尝试类似&lt;cfoutput&gt;#foo()#&lt;/cfoutput&gt; 之前不会做任何事情。

    在“默认函数”的情况下,这些只是 CF 在某些点调用的特殊函数/方法(如果它们存在)。

    【讨论】:

    • “触发这些也会导致它们的 -Start 对应项在下一页刷新时触发。”不对。调用事件处理程序不会触发实际的 event。在 JS 中,如果您调用 onclick 处理程序,鼠标按钮不会按下,是吗?不,您可能会从以下内容中受益:blog.adamcameron.me/2012/07/…
    • @AdamCameron 当然,你是对的。我不应该说的。虽然我从未尝试过它,但您是对的,事件处理程序本身不会触发事件。不久前通过我的手机修复了它。