7

In Grails 3.0 how do you map a URL to a file under the assets folder?

For example:
http://localhost:8080/favicon.ico --> grails-app/assets/images/bookmark.ico

I've tried a few test mappings, such as:

grails-app/controllers/UrlMappings.groovy

class UrlMappings {

   static mappings = {
      ...
      "/t1.png"  (uri: "/assets/images/test.png")
      "/t2.png"  (uri: "/assets/test.png")
      "/t3.png"  (uri: "/images/test.png")
      "/t4.png"  (dir: "assets/images", file: "test.png")
      ...
      }

}

...but they all result in a 500 server error.

Dem Pilafian
  • 5,625
  • 6
  • 39
  • 67

3 Answers3

0

I think you need to map the URL to AssetController of the Asset Plugin, like this (not tested):

class UrlMappings {
    static mappings = {

    ...

    "/files/$id"  (controller :"AssetController", action:"index") 
       ...
    }
}

HTH

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
YAT
  • 456
  • 1
  • 4
  • 14
  • That's an interesting approach, but it seems to only result in 404s (I additionally tried various other permutations using `AssetController` with no success). Do you have a specific working example? – Dem Pilafian Jul 16 '15 at 21:10
  • Actually not, that was just an idea. I had a short look at the `AssetController` an thought that could work... Maybe a look into the `AssetTagLib` helps you to understand the `AssetPlugin` and you can write your own Controller that the things you want to. – YAT Jul 17 '15 at 06:04
0

If you are not using the Asset-Pipeline you can map static resources to URLs by following the steps laid out in the Grails Documentation. However your question is asking how to map a single resource to a single URL with the Asset-Pipeline plugin.

Burt Beckwith provided insight on the Grails forum a few years ago about Grail's role in serving static resources.

Grails doesn't serve static resources, the container does. So there's no way to directly configure a mapping - you need to serve it through a controller or configure a proxy as Eric suggests.

Burt

This answer may be unsatisfying. But if you must serve a static resource and absolutely do not want to use a controller or proxy you can try the following.

Create a view called image.gsp. The view will only contain an asset tag. Using your examples above,

<asset:image src="t1.png"/>

Then configure your URL mappings to point to the image.gsp page.

class UrlMappings {

   static mappings = {
      ...
      "/t1.png"  (view: "image")
      ...
      }

}

I recognize this may not be the exact method you were hoping to use. But I think that understanding the role Grails plays vs. the container running Grails will help inform a decision to properly serve a resource to the user.

I know this may seem unrelated but if you want to create a page that lists the contents of a directory check out this post by CodePanda. His code can be used as a template to create a controller to serve a single file and he explains how to update the view, controller, and groovy.config.

Community
  • 1
  • 1
Nathan
  • 3,082
  • 1
  • 27
  • 42
  • Does this solution actually work? It looks very similar to `"/t3.png" (uri: "/images/test.png")` from the original question, and that does not work for me. Note that the question is about mapping a URL (**singular**) to a file (**singular**). – Dem Pilafian Jul 27 '15 at 22:53
  • I verified that it worked. Try mapping to a file outside of the asset folder. Place it in the grails project directory and see if you can map a URL to it. – Nathan Jul 27 '15 at 23:02
  • What URL did you use? (note: mapping to a file outside of the `assets` folder is of no value as then you don't even need the mapping.) – Dem Pilafian Jul 27 '15 at 23:18
  • 1
    The updated answer of using `` inside a GSP has the effect of displaying an image, but it looks like the HTTP response is technically HTML (`Content-Type:text/html;charset=UTF-8`). This solution may work for certain use cases, but it would not be applicable where the image file itself is required. – Dem Pilafian Sep 10 '15 at 00:16
-1

Using "/favicon.ico"(uri: "/assets/favicon.ico") works for me.

Conceptually, it appears that Asset Pipeline flattens the assets (so that you do not need to specity images, javascripts, stylesheets for the uri)

Grails: 3.2.11 AssetPipeline: 2.14.6

TolkienWASP
  • 309
  • 1
  • 3
  • 10