2010-07-03 76 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