应用程序变量没有被持久化

时间:2016-07-26 15:07:37

标签: coldfusion

问题:

我在同一个文件夹中有2个文件:

  • index.cfm
  • 的Application.cfc

我的index.cfm为空,下面是我的application.cfc代码:

<cfcomponent>
    <cffunction name="onApplicationStart">
        <cfapplication name="TimeHistory" clientmanagement="No" sessionmanagement="No" setclientcookies="Yes" />

        <cflock scope="application" type="exclusive" timeout=10>            
            <cfset application.test = "xxxxxxxxxxxxxx">
        </cflock>
        <cfreturn true />
    </cffunction>


    <cffunction name="onRequestStart">
        <cfdump var="#application#" label="before">     
        <cfif structKeyExists(URL,  "restart")>         
            <cfset onApplicationStart()>
        </cfif>
        <cfdump var="#application#" label="after">
        <cfabort>       
    </cffunction>
</cfcomponent>

如您所见,我在test中设置了application scope变量。

当我使用?restart URL查询参数调用页面时,这是输出:

网址: ../index.cfm?restart

enter image description here

接下来,我删除了?reset URL查询参数并加载了页面,这是输出:

网址: ../index.cfm

enter image description here

问题:

它是否应该将变量设置为第一个调用的一部分,然后保留以供将来调用?我怎么能这样做?

1 个答案:

答案 0 :(得分:4)

根据 + Leigh 的评论,我修改了现在可以使用的代码。留在这里作为解决方案。谢谢Leigh:)

<cfcomponent>
   <cfscript>
       this.name = "TimeHistory";
       this.clientmanagement= "no";
       this.sessionmanagement = "no";
       this.setClientCookies = "no";
       this.setDomainCookies = "no";
   </cfscript>
    <cffunction name="onApplicationStart">                
        <cflock scope="application" type="exclusive" timeout=10>            
            <cfset application.test = "xxxxxxxxxxxxxx">
        </cflock>
        <cfreturn true />
    </cffunction>


    <cffunction name="onRequestStart">
        <cfdump var="#application#" label="before">     
        <cfif structKeyExists(URL,  "restart")>         
            <cfset onApplicationStart()>
        </cfif>
        <cfdump var="#application#" label="after">
        <cfabort>       
    </cffunction>
</cfcomponent>