grails

Handle request with .gsp in URL


In Grails 6.2.3, I have to handle request with .gsp extension like http://localhost:8080/report/view.gsp?a=b

I can't map this URL to controller, action. It will always return error 404.

The default UrlMappings.groovy below should handle this case because .$format is optional. It should simply mapped to $controller and $action.

        "/$controller/$action?/$id?(.$format)?"{
        constraints {
            // apply constraints here
        }

I test URL with many other extensions and they are all work without any modification to UrlMappings.groovy. For example

This question suggests it should work, but it from Grails-3.3. Grails: URL mapping with .gsp extension/format


Solution

  • This is happening because your *.gsp paths are being handled by GroovyPagesServlet. To fix it, you need to remove GroovyPagesServlet:

    package stackoverflow
    
    class GroovyPagesServletMock {
    }
    
    import stackoverflow.GroovyPagesServletMock
    
    // Place your Spring DSL code here
    beans = {
        groovyPagesServlet(GroovyPagesServletMock)
    }
    

    I got it working this way. Hope this helps!

    enter image description here

    Sample https://github.com/mkmikael/stackoverflow79641438