Grails allows you to specify a custom 404 page simply using the UrlMappings.groovy class:
class UrlMappings {
static mappings = {
“500″(controller:”errors”, action:”serverError”)
“404″(controller:”errors”, action:”notFound”)
}
}
This allows you to direct to a controller where you can then forward to a view:
class ErrorsController {
def serverError = {
render(view:’/error’)
}def notFound = {
render(view:’/notFound’)
}
}
I started off with a simple view using the ‘main’ layout:
<html>
<head>
<meta name=”layout” content=”main” />
</head>
<body>
Page not found!
</body>
</html>
But for some reason, the layout does not get applied. I haven’t dug to the bottom of this yet, but I have found a work-around:
<g:applyLayout name=”main”>
<body>
Page not found!
</body>
</g:applyLayout>
Now the layout is applied and the page looks like I would expect.
The error page for ‘500′ is fine, it only seems to affect the 404 page.
(Grails 1.0-RC1Â & Grails 1.0-RC2)




{ 7 comments… read them below or add one }
btw you should add your blog to http://www.groovyblogs.org/ so others in the groovy community can check it out
I’ve done this now, thanks for the nudge.
Paul, I think the problem with the layout not being applied is due to the sitemesh filter not being executed during a sendError (should check Servlet API specification to be sure).
I think that could be fixed adding a ERROR to the filter mapping.
My 2C.
Regards,
The “error” should be between dispatcher tags that were not posted
.
It turns out it is a known issue. Graeme replied when I posted to the user group. I guess I was thrown a bit because it works for the 500 errors, so far I’ve only had problems with the 404.
Thanks Paul for putting this information together.
As additional information to the user I would like the error message (set by sendError in my controller). Unfortunately the Servlet API does not allow to easily retrieve it?
How much ‘dynamics’ did you put into your error templates?
Thanks,
Niko
PS: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#sendError(int,%20java.lang.String)
Niko, all I really did was log the error in a database table in ErrorsController. The views were static. By logging to the database, I can have a job email me the significant events at the end of the day – or as they happen (for critical events).