Shashank Agrawal
Shashank Agrawal

Reputation: 25797

Rendering static file as URI in Grails 3

In Grails 2.x, the following works:

class UrlMappings {

    static mappings = {
        /**
         * Serving the index.html directly
         */
        "/"(uri: "/ng/app/index.html")
    }
}

Given that there is a index.html file available in web-app/ng/app/ directory. Now when we browse the URL http://localhost:8080 in Grails 2, the index.html renders automatically.

In Grails 3, I added the same index.html file in src/main/webapp/, I can properly browse the same like http://localhost:8080/static/index.html.

So, I'm trying to do the same in UrlMappings.groovy:

class UrlMappings {

    static mappings = {
        /**
         * Serving the index.html directly
         */
        "/"(uri: "/static/index.html")
    }
}

But this is giving me error {"message":"Internal server error","error":500}:

ERROR org.grails.web.errors.GrailsExceptionResolver - UrlMappingException occurred when processing request: [GET] /
Unable to establish controller name to dispatch for [null]. Dynamic closure invocation returned null. Check your mapping file is correct, when assigning the controller name as a request parameter it cannot be an optional token!. Stacktrace follows:
grails.web.mapping.exceptions.UrlMappingException: Unable to establish controller name to dispatch for [null]. Dynamic closure invocation returned null. Check your mapping file is correct, when assigning the controller name as a request parameter it cannot be an optional token!
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

I've create my app with Grails 3.1.8 Angular profile and later removed Angular related stuff like Grails GSP plugin, Asset pipeline etc.

Upvotes: 2

Views: 1955

Answers (2)

Nikhil Sharma
Nikhil Sharma

Reputation: 941

As of Grails 3.x's implementation of URLMapping handler, you can only specify controller, view or a redirect in your URL mappings. As such your uri is going to fail as it does not map to any controller or view.

Though after tinkering with the source code, I found a workaround that may come in handy for your use case here. You can actually issue a redirect here because redirect will be able to handle uri.

So, your URLMappings.groovy should be like -->

class UrlMappings {

    static mappings = {
        /**
        * Serving the index.html directly
        */
        "/"(redirect: [uri: "/static/index.html"])
    }
}

This code works fine for me with Grails 3.1.8.

I hope this helps.

Upvotes: 1

Skurhse Rage
Skurhse Rage

Reputation: 1020

This appears to be an open issue with Grails 3, slated to be resolved for Grails 3.2.0.

https://github.com/grails/grails-core/issues/9908

Upvotes: 1

Related Questions