2010-07-03 75 views

回答

4

我通常將錯誤代碼處理程序重新路由到控制器,以便在渲染視圖之前執行一些日誌記錄或任​​何其他操作。您可以使用這裏也:

class UrlMappings { 

    static mappings = { 

     "/searchable/$action?"(controller: "errors", action: "urlMapping") 

     "/$controller/$action?/$id?" { } 

     "/"(view:"/index") 

     "403"(controller: "errors", action: "accessDenied") 
     "404"(controller: "errors", action: "notFound") 
     "405"(controller: "errors", action: "notAllowed") 
     "500"(view: '/error') 
    } 
} 

其中ErrorsController看起來是這樣的:

class ErrorsController { 

    def accessDenied = {} 

    def notFound = { 
     log.debug "could not find $request.forwardURI" 
    } 

    def notAllowed = {} 

    def urlMapping = { 
     log.warn "unexpected call to URL-Mapped $request.forwardURI" 
     render view: 'notFound' 
    } 
} 

,您需要在grails-創建accessDenied.gsp,notFound.gsp和notAllowed.gsp應用程序/錯誤

通過向其自定義映射發送「隱藏」控制器,您可以記錄對其的意外訪問,但仍會呈現404頁面以隱藏其存在。

+0

這是一個好主意,那麼我可以使它看起來像任何其他資源未找到錯誤。我喜歡!我創建了一個/views/searchable/index.gsp來覆蓋插件中包含的一個,但我只是擺脫這一點,這樣做。謝謝! – 2010-07-04 03:47:40

+0

@Burt - 有什麼方法可以在啓動時禁用/刪除UrlMapping?這將是一個更優雅的解決方案。 – 2012-01-22 23:23:38

+0

你可能會刪除它,但我懷疑它會是一個簡單的修復。看起來像一個功能請求的好候選人。當我們執行控制器命名空間時,它可能會實現(暫定爲v2.2)。 – 2012-01-22 23:30:40