我們已經在我們的Application.cfc下面的代碼:ColdFusion的「套路不能多次聲明」
<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>
它導致在屏幕上打印單詞「是」。這表明上述內容應該起作用,因爲if語句的內容顯然會評估爲「是」,因此if語句將評估爲false,因此函數將不會被聲明,因此我將保持我的理智。但由於某種原因,這個問題依然存在。
有關可能發生什麼或如何解決下一個問題的任何想法?我被困在這一點上。
你確定error.cfm沒有被包含在elswhere?它只包含在onError()中嗎? – 2012-04-02 18:15:19
是的,它只在onError()中。 – Nicholas 2012-04-02 19:37:47