Nic*_*las 6 coldfusion application.cfc coldfusion-9
我们的 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>
Run Code Online (Sandbox Code Playgroud)
在 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>
Run Code Online (Sandbox Code Playgroud)
这是脚本的一部分,该脚本将有关错误的大量详细信息放在一起并将其记录到数据库中。
发生错误时,我们会收到消息“例程 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>
Run Code Online (Sandbox Code Playgroud)
和:
<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>
Run Code Online (Sandbox Code Playgroud)
两者都不起作用。我还尝试在函数调用之前将其添加到页面中:
<cfoutput>"#StructKeyExists(variables,"GetCurrentURL")#"</cfoutput>
Run Code Online (Sandbox Code Playgroud)
它导致屏幕上打印出“是”一词。这表明上面的内容应该有效,因为显然 if 语句的内容将计算为“YES”,因此 if 语句将计算为 false,因此该函数将不会被声明,因此我将保持理智。但由于某种原因,这个问题仍然存在。
对可能发生的情况或接下来如何排除故障有什么想法吗?我被困在这一点上。
ColdFusion 在将其编译为字节码时仍然会看到函数声明。您可以使用 cfinclude 来包含函数声明:
<cfif StructKeyExists(variables,"GetCurrentURL") IS "NO">
<cfinclude template="udf.cfm" />
</cfif>
Run Code Online (Sandbox Code Playgroud)
然后在 udf.cfm 中放置您的函数声明。这应该可以按照您的要求工作并防止 CF 抛出错误。