2

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.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • 1
    Please see: http://stackoverflow.com/questions/31441643/urlmappings-to-point-a-url-to-an-asset-pipeline-file-in-grails – srage Jul 02 '16 at 00:37
  • Thanks @DrewBeres for replying. I already knew this solution and I've another way of serving the static content via file stream but unfortunately I want that solution to work like Grails 2.5.4 – Shashank Agrawal Jul 03 '16 at 05:12

2 Answers2

1

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

srage
  • 990
  • 1
  • 9
  • 27
1

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.

Nikhil Sharma
  • 891
  • 1
  • 8
  • 11
  • Unfortunately, this doesn't seem to be working for me. It is still redirecting me to the `/static/index.html` instead of serving `index.html` directly. – Shashank Agrawal Jul 06 '16 at 12:08
  • As of now, you can't simply serve static files this way. But if you use the `redirect` with `uri`, it will serve without throwing the exception you were getting. This is just a workaround if you want to do the same using URLMapping. – Nikhil Sharma Jul 06 '16 at 12:18
  • Oh! I though your workaround is not for redirecting. This is already working for me as `"/"(redirect: '/ng/app/index.html')` – Shashank Agrawal Jul 06 '16 at 12:23
  • 1
    Exactly, this will at least save us from using a controller just to serve index page if our application is SPA – Nikhil Sharma Jul 06 '16 at 12:31