测试 ColdFusion 中 FORM 范围/结构是否存在

发布于 2024-07-30 03:36:44 字数 1540 浏览 4 评论 0原文

问题:请求 WSDLCFC,我收到以下错误:变量 FORM 未定义。 它发生在这行代码中,在 application.cfc 的 OnRequestStart 方法中,

<cfif structKeyExists(form,'resetappvars')>
    <cfset OnApplicationStart() />
</cfif>

如果我请求特定方法,它工作正常。 我考虑过使用 cfparam 创建默认表单结构(如果不存在),但这看起来像是一个丑陋的 hack,我担心它实际上会在变量或 CFC 的此范围中创建表单结构。 也许这也是一个合法的错误?

注意: 仅当我请求 WSDL 时,如果我直接调用方法,才会发生这种情况 - 代码按预期执行,不会出现问题。

更新: Application.cfc 代码示例 - 只需将任何 CFC 添加到您的应用程序并使用 ?wsdl 请求即可查看问题。 这已在 ColdFusion 7 和 ColdFusion 8 上进行了测试(但失败)。

<cfcomponent output="false">

    <cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
        <cfset application.dsn = "my_dsn" />
        <cfreturn true />
    </cffunction>

    <cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
        <cfargument name="TargetPage" type="string" required="true" />
        <cfif structKeyExists(form,'resetappvars')>
            <cfset OnApplicationStart() />
        </cfif>
        <cfreturn true />
    </cffunction>
</cfcomponent>

Problem: When requesting the WSDL for a CFC, I get the following error: Variable FORM is undefined. It happens in this line of code, in the OnRequestStart method in application.cfc

<cfif structKeyExists(form,'resetappvars')>
    <cfset OnApplicationStart() />
</cfif>

If I request a specific method, it works fine. I have considered using cfparam to create a default form struct if none exists, but that seems like an ugly hack and I worry it will actually create the form struct in the variables or this scope of the CFC. Maybe this is a legitimate bug as well?

Note: This only happens when I request the WSDL, if I invoke a method directly - the code executes as expected without problems.

Update: Application.cfc code sample - just add any CFC to your app and request it with ?wsdl to see the issue. This has been tested (and failed) on ColdFusion 7 and ColdFusion 8.

<cfcomponent output="false">

    <cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
        <cfset application.dsn = "my_dsn" />
        <cfreturn true />
    </cffunction>

    <cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
        <cfargument name="TargetPage" type="string" required="true" />
        <cfif structKeyExists(form,'resetappvars')>
            <cfset OnApplicationStart() />
        </cfif>
        <cfreturn true />
    </cffunction>
</cfcomponent>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遗弃M 2024-08-06 03:36:45

您还可以cfparam您正在寻找的变量,然后稍微改变一下您的逻辑(假设resetAppVars是一个布尔值:

<cfparam name="form.resetAppVars" default="false" />
...
<cfif form.resetAppVars>
  <cfset OnApplicationStart() />
</cfif>

编辑:我不确定上面的代码是否可以被视为黑客,但是对我来说,这似乎是相当标准的CF。

You could also cfparam the variable you're looking for then just change your logic a little (assuming resetAppVars is a boolean:

<cfparam name="form.resetAppVars" default="false" />
...
<cfif form.resetAppVars>
  <cfset OnApplicationStart() />
</cfif>

Edit: I'm not sure if the above code could be considered a hack, but it seems pretty standard CF, to me.

看春风乍起 2024-08-06 03:36:45

Ben Nadel 的帖子 提供了可用范围的详细列表不同类型的请求。

通过阅读它,您可以轻松发现 form 范围在给定上下文中不可用,但 url 可用。

This post of Ben Nadel gives detailed list of scopes available for different types of requests.

By reading it you can easily find out that form scope is not available in given context, but url is.

三生池水覆流年 2024-08-06 03:36:45

我听说这只是一个意见问题,但在我看来,在 CFC 中引用表单作用域是不合适的,因为不能保证在调用您的 cfc 以及调用您的方法时表单作用域可用叫做。 最好确保该方法所需的任何数据都明确提供给您的对象。 这可以通过包含一个参数来完成:

<cfargument name="resetAppVars" type="boolean" required="false" default="false" />

然后检查arguments.resetAppVars,它总是被定义的,但默认为 false。

或者通过在对象上创建属性并创建显式设置方法:(

在 cfc 顶部)

<cfset this.resetAppVars = false />


<cffunction name="setResetAppVars" access="public" returnType="void" output="false">
   <cfargument name="flagValue" type="boolean" required="true" />

   <cfset this.resetAppVars = arguments.flagValue />
</cffunction>

在这种情况下,您将检查 this.resetAppVars。 您还可以使用 作为声明在本地范围化它,这使其成为对象的私有属性,并且可能是正确的,因此调用该对象的代码不能使用非布尔类型不正确地覆盖此变量。 在这种情况下,您只需在测试中直接引用resetAppvars,而不是使用此范围。

I've heard it's just a matter of opinion, but it seems to me that it is improper to reference your form scope within a CFC, as there is no guarantee that the form scope will be available when your cfc is invoked and when your method is called. It is better to ensure that any data that needs to be available to the method is provided explicitly to your object. This can be done either by including an argument:

<cfargument name="resetAppVars" type="boolean" required="false" default="false" />

Then you check arguments.resetAppVars, and it is always defined, but defaulted to false.

Or by creating an attribute on your object and creating an explicit set method:

(at the top of your cfc)

<cfset this.resetAppVars = false />


<cffunction name="setResetAppVars" access="public" returnType="void" output="false">
   <cfargument name="flagValue" type="boolean" required="true" />

   <cfset this.resetAppVars = arguments.flagValue />
</cffunction>

In which case you will check against this.resetAppVars. You can also scope this locally using <cfset var resetAppVars = false /> as the declaration, which makes it a private attribute of your object, and is probably proper, so code that invokes the object cannot improperly overwrite this variable with a non-boolean type. In that case, you would simply refer directly to resetAppvars in your test, instead of using this scope.

灰色世界里的红玫瑰 2024-08-06 03:36:45

您也可以这样做:

<cfif NOT isSoapRequest()>...

并将剩余的逻辑粘贴到该块中。

You could also do this:

<cfif NOT isSoapRequest()>...

and stick your remaining logic inside that chunk.

画骨成沙 2024-08-06 03:36:44

添加一个: ?

 <cfif IsDefined("form")>...</cfif>

也许尝试在上面的代码周围

Maybe try adding a:

 <cfif IsDefined("form")>...</cfif>

around the above code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文