Home > Java > 404 with Grails

404 with Grails

by paul on December 6, 2007

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 }

Graeme Rocher December 6, 2007 at 4:31 am

btw you should add your blog to http://www.groovyblogs.org/ so others in the groovy community can check it out

paul December 7, 2007 at 12:28 am

I’ve done this now, thanks for the nudge.

Pablo Saavedra December 8, 2007 at 3:40 am

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,

Pablo Saavedra December 8, 2007 at 3:41 am

The “error” should be between dispatcher tags that were not posted :P .

paul December 8, 2007 at 1:37 pm

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.

Niko Schmuck March 4, 2008 at 11:42 am

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)

paul March 5, 2008 at 12:44 am

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).

Leave a Comment

Previous post:

Next post: