2

我想在Application.cfc中的onMissingTemplate()方法中處理ColdFusion應用程序中的404錯誤。使用onMissingTemplate()無法處理Coldfusion 8中的404錯誤

我的開發環境是ColdFusion 8(開發版)+ JRun + Linux。在[WEB_ROOT] /testmissing/Application.cfc我有代碼CF文檔:

<cfcomponent> 

    <cffunction name="onMissingTemplate"> 
     <cfargument name="targetPage" type="string" required=true/> 

     <!--- Use a try block to catch errors. ---> 
     <cftry> 
      <!--- Log all errors. ---> 
      <cflog type="error" text="Missing template: #Arguments.targetPage#"> 

      <!--- Display an error message. ---> 
      <cfoutput> 
       <h3>#Arguments.targetPage# could not be found.</h2> 
       <p>You requested a non-existent ColdFusion page.<br /> 
       Please check the URL.</p> 
      </cfoutput> 

      <cfreturn true /> 

      <!--- If an error occurs, return false and the default error 
      handler will run. ---> 
      <cfcatch> 
       <cfreturn false /> 
      </cfcatch> 
     </cftry> 

    </cffunction> 
</cfcomponent> 

當我瀏覽/testmissing/foo.cfm我得到默認CF 「找不到文件...」 錯誤頁面。

我在不同的環境下嘗試了相同的代碼:Coldfusion 9(開發版)+ Apache + Windows。如預期的那樣,瀏覽/testmissing/foo.cfm導致從onMissingTemplate發出custome錯誤消息。

這證明代碼是好的,問題在我的開發環境中,但我找不到它。任何想法,爲什麼我仍然得到默認的CF錯誤頁面?

回答

0

您的CF管理員中的錯誤處理程序設置如何?比較你的CF8和CF9環境。

+0

它們在CF8和CF9環境中都是空的。生產服務器與其他CF應用程序共享,我不想捕捉他們的404錯誤。所以我試圖在應用程序級別上處理404。 – Michlis 2011-03-15 12:20:31

0

你嘗試過沒有try/catch嗎?只是想確保你不會模糊cflog的問題。

+0

是的,我嘗試沒有嘗試/ catch塊,但沒有運氣。我注意到一個奇怪的行爲。如果僅在URL中放入/ testmissing /比onMissingTemplate()被觸發(我在testmissing中沒有索引文件)。但是,/testmissing/foo.cfm或anythig其他dosn't不會觸發onMissingTemplate()。我不得不放棄並通過服務器端的重寫規則來解決它:| – Michlis 2011-03-21 09:00:07