ahanson
ahanson

Reputation: 2178

Problems with Grails 404 UrlMapping

I'm having some problems getting Grails to map my 404 errors to an errors controller like in the documentation. I'm running Grails 1.3.5 and when I add the following mapping to a default application:

"404" (controller:'errors', action:'notFound')

It works for mapping 500 errors but not 404's. I seem to recall having this problem before and it being related to Tomcat (vs Jetty) but I don't remember a fix or I thought it might have been resolved by now.

I try accessing a resource that's not defined like http://localhost:8080/appName/controllerName/blah and all I get is the default Tomcat 404.

I'm doing a standard grails run-app for testing and trying to get it to work.

Upvotes: 8

Views: 4468

Answers (3)

Aaron Saunders
Aaron Saunders

Reputation: 33345

http://jira.codehaus.org/browse/GRAILS-4232 I think this is a known issue

Upvotes: 0

Kerem
Kerem

Reputation: 993

after deleting whitespace, the problem solved

"404"(controller:'errors', action:'notFound')

Upvotes: 8

robbbert
robbbert

Reputation: 2193

Add the following code to your application's scripts/Events.groovy:

import groovy.xml.StreamingMarkupBuilder

//modify the generated web.xml so that it supports being mapped to 'error'
eventWebXmlEnd = {String tmpfile ->
    //find the filter mapping to change
    String filterNm = "grailsWebRequest"
    def root = new XmlSlurper().parse(webXmlFile)
    def gwr = root."filter-mapping".find { it."filter-name" == filterNm }
    if (!gwr.size()) throw new RuntimeException(
        "[fail] No Filter named $filterNm")

    // xml is as expected, now modify it and write it back out
    gwr.appendNode {
        dispatcher("ERROR")
    }
    // webXmlFile is an implicit variable created before event is invoked
    webXmlFile.text = new StreamingMarkupBuilder().bind {
        mkp.declareNamespace("": "http://java.sun.com/xml/ns/j2ee")
        mkp.yield(root)
    }
}

See this post for an explanation. Note that I've copied the above script from that posting, but it had to be amended as the web.xml's structure appears to have changed since the time of the posting's writing.

Upvotes: 0

Related Questions