UrlMappings to point a URL to an asset pipeline file in Grails

白昼怎懂夜的黑 提交于 2019-12-04 05:01:52

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.

YAT

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!