ColdFusion中的全局变量 [英] Global Variables in ColdFusion

查看:143
本文介绍了ColdFusion中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2问题 -



在CF8中的application.cfm中我冷设置一个全局变量
,如下所示

 < cfset DSN =dej6_42> 

我现在试图调整CF10 / Lucee中的Application.cfc,

 <$ c $ 

c>< cfcomponent output =false>

< cfset This.name =我的应用程序>
< cfset This.clientmanagement =True>
< cfset This.loginstorage =Session>
< cfset This.sessionmanagement =True>
< cfset This.sessiontimeout =#createtimespan(0,0,10,0)#>
< cfset This.applicationtimeout =#createtimespan(5,0,0,0)#>

< cfset DSN =dej6_42>
< / cfcomponent>

我试过了

 < cfset this.DSN =dej6_42> 

然后尝试在单独的页面中调用

 < cfoutput>#Applicaton.DSN#< / cfoutput> 

我认为从我的研究,我需要使用application.cfc和application.cfm来完成以上。 * edit - 我试图在application.cfc文件的末尾添加一个include到applciation.cfm,但它没有工作。



2问题。
当我在Application.cfc中放置任何标准函数时,我的网站变为一个空白页



这里是Application.cfc - 如果我删除

 < cfcomponent output =false> 

< cfset This.name =我的应用程序>
< cfset This.clientmanagement =True>
< cfset This.loginstorage =Session>
< cfset This.sessionmanagement =True>
< cfset This.sessiontimeout =#createtimespan(0,0,10,0)#>
< cfset This.applicationtimeout =#createtimespan(5,0,0,0)#>

< cfset DSN =dej6_42>

< cffunction name =onApplicationStart>

< / cffunction>

< cffunction name =onApplicationEnd>

< / cffunction>


< cffunction name =onRequestStart>

< / cffunction>

< cffunction name =onRequest>

< / cffunction>


< cffunction name =onRequestEnd>

< / cffunction>

< cffunction name =onSessionStart>

< / cffunction>

< cffunction name =onSessionEnd>

< / cffunction>

< cffunction name =onError>

< / cffunction>



< / cfcomponent>


解决方案

您的示例不设置全局变量。它在变量范围中设置一个变量:任何基于CFC的代码和请求中使用的任何自定义标签都不能访问它。它只会在Application.cfm,请求的文件,它包含的文件和OnRequestEnd.cfm



中可用。Application.cfc是一个CFC ),因此其中设置的变量范围变量仅在其中可用。如果要设置一个应用程序范围的变量,则需要将其放在应用程序范围内。应用程序范围变量应在 onApplicationStart()处理程序中设置,该程序在应用程序启动时运行一次,但不在每个请求上运行。通过比较Application.cfm(错误命名)在每个请求上运行。它应该叫OnRequestStart.cfm。



如果你使用 onRequest()拦截器, c $ c> include 原始请求的文件,那么请求将在Application.cfc实例的上下文中运行,并且变量设置在 onRequest()将可用于主线请求代码的其余部分,非常类似于在Application.cfm中设置变量的方式。语义上,如果你的意思是一个变量存在的应用程序的生命(如DSN),然后把它放在应用程序范围是最好的赌注。



它听起来对我从推理,可以从你的问题,你的应用程序架构可能在90年代徘徊。我认为您应该阅读使用框架(例如: FW / 1 ColdBox )以更好地以可维护和可扩展的方式组织您的代码。



另外,您应该阅读 Application.cfc (以及方法参考 a>)。一般可能是CFC:使用ColdFusion组件 - 开发指南



您也可以考虑现代化编写CFML的方法,并为视图代码和其他方式使用脚本备用标签。它使代码更容易跟随你和其他开发人员,如果整个应用程序不混乱的标签,最终可能需要维护它。


2 Questions -

In CF8 in the application.cfm I cold set a global variable like so

<cfset DSN = "dej6_42">

I am now trying to adjust to the Application.cfc in CF10/Lucee and can not figure out how to set this same variable.

Here is my current Application.cfc

<cfcomponent output="false"> 

    <cfset This.name = "My Application"> 
    <cfset This.clientmanagement="True"> 
    <cfset This.loginstorage="Session"> 
    <cfset This.sessionmanagement="True"> 
    <cfset This.sessiontimeout="#createtimespan(0,0,10,0)#"> 
    <cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">

    <cfset DSN = "dej6_42">
</cfcomponent>

I have tried

<cfset This.DSN = "dej6_42">

Then tried to call in a separate page

<cfoutput>#Applicaton.DSN#</cfoutput>

I think from my research I will need to use both application.cfc and application.cfm to accomplish the above. *edit - I tried to add an include at the end of the application.cfc file to applciation.cfm and it did not work.

2 Question. When I place any of the standard functions in the Application.cfc my site turns to a blank page

Here is that Application.cfc - I if I remove everything below the DSN set then it will display the site.

<cfcomponent output="false"> 

    <cfset This.name = "My Application"> 
    <cfset This.clientmanagement="True"> 
    <cfset This.loginstorage="Session"> 
    <cfset This.sessionmanagement="True"> 
    <cfset This.sessiontimeout="#createtimespan(0,0,10,0)#"> 
    <cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">

    <cfset DSN = "dej6_42">

    <cffunction name="onApplicationStart"> 

    </cffunction> 

    <cffunction name="onApplicationEnd"> 

    </cffunction> 


    <cffunction name="onRequestStart">  

    </cffunction> 

    <cffunction name="onRequest"> 

    </cffunction> 


    <cffunction name="onRequestEnd"> 

    </cffunction> 

    <cffunction name="onSessionStart"> 

    </cffunction> 

    <cffunction name="onSessionEnd"> 

    </cffunction> 

    <cffunction name="onError"> 

    </cffunction> 



</cfcomponent>

解决方案

Your example doesn't set a global variable. It sets a variable in the variables scope: it will not be accessible to any CFC-based code nor any custom tags used within the request. It'll only be available in the Application.cfm, the file requested, files it includes, and OnRequestEnd.cfm

Application.cfc is a CFC (to state the obvious), so variables-scoped variables set within it are only available within it. If you want to set an application-wide variable, you need to put it in the application scope. Application scope variables should be set in the onApplicationStart() handler which is run once when the application starts, but not on every request. By way of comparison Application.cfm (which is misnamed) is run on every request. It should be called OnRequestStart.cfm.

If you use an onRequest() interceptor, and within that include the originally requested file, then the request will be run in the context of the Application.cfc instance, and variables set within onRequest() will be available to the rest of the mainline request code, much like the way you set your variable in Application.cfm. Semantically though, if you mean a variable to exist for the life of the application (like a DSN), then putting it in the application scope is the best bet.

It sounds to me from the inferences one can make from your question that your app architecture might be languishing in the 1990s. I think you should read up on using a framework (eg: FW/1 or ColdBox) to better organise your code in a maintainable and scalable way.

Also you should read up on Application.cfc (and method reference). And probably CFCs in general: Using ColdFusion components-Developing guide.

You also might want to think about modernising your approach to writing CFML and spare the tags for view code, and otherwise using script. It makes the code easier to follow for both you and other developers who might end up needing to maintain it if the whole app isn't cluttered up with tags.

这篇关于ColdFusion中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆