{"id":28760,"date":"2015-10-22T12:31:41","date_gmt":"2015-10-22T07:01:41","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=28760"},"modified":"2015-10-22T12:36:59","modified_gmt":"2015-10-22T07:06:59","slug":"generating-grails-static-resource-link-manually","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/generating-grails-static-resource-link-manually\/","title":{"rendered":"Generating Grails static resource link manually"},"content":{"rendered":"<p>In case you are using Grails resources plugin and at some point you need to generate the static resource link in some service or ordinary &#8216;src&#8217; class &#8211; following is a quick code that can help you getting the resources link\/URL (by using resources tag lib only). The good part is that it&#8217;s not a hard-coded URL, so it will follow all the standards\/settings configured around the Grails <a href=\"http:\/\/grails.org\/plugin\/resources\">resources-plugin<\/a>. <\/p>\n<p>[code]<br \/>\ndef grailsApplication<br \/>\nString getStaticResourceLink(String uri){<br \/>\n        if(uri){<br \/>\n            def resourceTagLib = grailsApplication.mainContext.getBean(&quot;org.grails.plugin.resource.ResourceTagLib&quot;)<br \/>\n            def rLinkResource = resourceTagLib ? resourceTagLib.resolveLinkUriToUriAndResource(uri: uri): null<br \/>\n            if(rLinkResource)<br \/>\n                uri = rLinkResource.uri<br \/>\n        }<br \/>\n        return uri<br \/>\n    }<br \/>\n[\/code]<\/p>\n<p>Usually, resources plugin automatically renders the URLs in an application. But this was a very special requirement where I was required to generate the link manually. Another condition was to follow all the resources-plugin&#8217;s settings because it does a lot of work before generating the (dynamic\/accurate\/relevant) links. So this worked very well &#8211; even for the CDN configurations.<br \/>\nThe only thing you need to take care is to pass the correct &#8220;uri&#8221; otherwise (if resource not found at the given location\/uri) you may get a broken link.<\/p>\n<p>I hope it will help someone. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In case you are using Grails resources plugin and at some point you need to generate the static resource link in some service or ordinary &#8216;src&#8217; class &#8211; following is a quick code that can help you getting the resources link\/URL (by using resources tag lib only). The good part is that it&#8217;s not a [&hellip;]<\/p>\n","protected":false},"author":606,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7,1],"tags":[2648],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/28760"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/606"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=28760"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/28760\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=28760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=28760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=28760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}