5

我们的 Application.cfc 中有以下代码:

<cffunction name="onError" returnType="void" output="false">
    <cfargument name="exception" required="true">
    <cfargument name="eventname" type="string" required="true">
    <cfset cfcatch = exception>
    <cfinclude template="standalone/errors/error.cfm">
</cffunction>

在 error.cfm 页面中,我们有这段代码(不是我写的):

<cfscript>
        function GetCurrentURL() {
            var theURL = "http";
            if (cgi.https EQ "on" ) theURL = "#TheURL#s";
            theURL = theURL & "://#cgi.server_name#";
            if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#";
            theURL = theURL & "#cgi.path_info#";
            if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#";
            return theURL;  
        }
</cfscript>

这是一个脚本的所有部分,它将有关错误的大量详细信息组合在一起并将其记录到数据库中。

发生错误时,我们会收到消息“例程 GetCurrentURL 已在不同模板中声明了两次”。但是,我以几种不同的方式搜索了整个代码库,发现“GetCurrentURL”只使用了两次,两次都是在 error.cfm 中。第一次是声明,第二次是实际使用。所以我不确定为什么 CF 说“在不同的模板中”。

我的下一个想法是问题是递归调用,并且 error.cfm 出错并调用自身,所以我尝试了这两个更改,其中任何一个都应该解决问题并揭露真正的错误:

<cfif StructKeyExists(variables,"GetCurrentURL") IS "NO">
    <cfscript>
            function GetCurrentURL() {
                var theURL = "http";
                if (cgi.https EQ "on" ) theURL = "#TheURL#s";
                theURL = theURL & "://#cgi.server_name#";
                if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#";
                theURL = theURL & "#cgi.path_info#";
                if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#";
                return theURL;  
            }
    </cfscript>
</cfif>

和:

<cfscript>
    if (!StructKeyExists(variables,"GetCurrentURL")) {
            function GetCurrentURL() {
                var theURL = "http";
                if (cgi.https EQ "on" ) theURL = "#TheURL#s";
                theURL = theURL & "://#cgi.server_name#";
                if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#";
                theURL = theURL & "#cgi.path_info#";
                if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#";
                return theURL;  
            }
    }
</cfscript>

都没有奏效。我还尝试在函数调用之前将其添加到页面中:

<cfoutput>"#StructKeyExists(variables,"GetCurrentURL")#"</cfoutput>

它导致屏幕上打印出“YES”字样。这表明上述应该有效,因为显然 if 语句的内容将评估为“YES”,因此 if 语句将评估为 false,因此不会声明该函数,因此我将保持理智。但由于某种原因,这个问题仍然存在。

关于可能发生的事情或下一步如何排除故障的任何想法?我被困在这一点上。

4

2 回答 2

5

ColdFusion 在将其编译为字节码时仍会看到函数声明。您可以使用 cfinclude 来包含函数声明:

<cfif StructKeyExists(variables,"GetCurrentURL") IS "NO">
<cfinclude template="udf.cfm" />
</cfif>

然后在 udf.cfm 中放置您的函数声明。这应该可以按您的意愿工作,并防止 CF 抛出错误。

于 2012-04-02T19:09:26.360 回答
-1

另一种解决方案是在定义之前从范围中删除函数。例如...

<cfset StructDelete(variables,'myFunction')>
<cffunction name="myFunction">...</cffunction>
于 2017-01-19T13:02:48.823 回答