|
Hi all, I saw this JIRA issue about relative protocols in resource declaration. And I have no idea how to use relative protocol in grails resources.
My environment is an application that runs in http and also in https, so resources should be loaded through both protocols, depending on which user is using. Then, if user is accessing through http, resources should be loaded through http, but if user is accessing through https, resources should be loaded through https, in order to avoid browser messages like "This page contains untrusted resources", or similar.
In a GSP I have the code: ${resource(dir:"css", file:"style.css", absolute:"true")} That renders: <link rel='stylesheet' type='text/css' href='http://localhost/myapp/css/style.css' />
What I want to get is: <link rel='stylesheet' type='text/css' href='//localhost/myapp/css/style.css' /> (href without protocol definition, then browser will take care of load resource with proper protocol like described above)
I tried passing some properties in ${resource()} to get that behavior, but failed. Anyone had this same issue? Maybe I am wrong about the cause of that browser messages?
Cheers, Caio.
|
|
This post was updated on .
Hi, Caio.
For references to local resources you should want to get "href" attribute without host and protocol part. Just plain absolute or relative local references. Something like this : <link rel='stylesheet' type='text/css' href='/myapp/css/style.css' />The effect will be the same as with your variant. Protocol relative URLs simplifies rendering links to external site's resources, CDN for jquery or like. Currently I use it statically in layout gsp for my grails applications. Small template like this: grails-app/views/common/html/_js.gsp: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery/jquery-1.6.4.min.js"><\/script>')</script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script>window.jQuery.ui || document.write('<script src="/js/jquery/jquery-ui-1.8.16.min.js"><\/script>')</script>included in grails-app/views/layouts/home-page.gsp :
<!doctype html>
<!--[if IE 7 ]> <html lang="ru" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8 ]> <html lang="ru" class="no-js ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="ru" class="no-js"> <!--<![endif]-->
<head>
...
</head>
<body>
...
<g:layoutBody />
...
<tmpl:/common/html/js />
</body>
Hope this helps. Cheers, Igor.
Justify your existence !
|
|
In reply to this post by caiofbpa@gmail.com
I think the problem is the absolute:"true" in
${resource(dir:"css", file:"style.css", absolute:"true")} do you really need this? Christian
2011/10/24 [hidden email] <[hidden email]>
|
|
Thanks for the replies!
Actually, I need to get those URL being generated dynamically because I have many environments, each one with it own FQDN, and I also need it absolute. I understand that my scenario isn't the most usual, then I'm considering to build resource links by hand, but for that I need access to get hostname set per environment in Config.groovy. I need to get programmatically access to that, in order to build "//${hostname}/${dir}/${file}".
I'll try to get that working and report here. :) Cheers, Caio. On Mon, Oct 24, 2011 at 5:12 PM, Christian Weninger <[hidden email]> wrote: I think the problem is the absolute:"true" in |
|
I want to override resource() method, but since it isn't a plugin anymore, I have no idea how to do that decently (to be available in all files instead of only one).
Could someone show me a good way?
Cheers, Caio. On Tue, Oct 25, 2011 at 7:58 AM, [hidden email] <[hidden email]> wrote: Thanks for the replies! |
|
On 27 Oct 2011, at 14:01, [hidden email] wrote: > I want to override resource() method, but since it isn't a plugin anymore, I have no idea how to do that decently (to be available in all files instead of only one). > > Could someone show me a good way? I don't think is feasibly to replace an existing Grails tag in a reliable way. You are talking about replacing g.resource tag - that is what the method calls. I suggest you just create your own my.resource tag and use that. That will not mean plugins use it of course, but it depends if that matters to you. Marc ~ ~ ~ Marc Palmer Freelancer (Grails/Groovy/Java) Founder @ http://noticelocal.com | Developer @ http://weceem.org Member @ http://spottymushroom.com | Contributor @ http://grails.org Twitter: http://twitter.com/wangjammer5 | Resumé: http://www.anyware.co.uk/marc/ Blog: http://www.anyware.co.uk | Grails Rocks: http://www.grailsrocks.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by caiofbpa@gmail.com
I solved my problem, the simplest way possible.
Instead of overriding resource() and digging into Grails as an intruder, I just analyzed current behavior of resource() method. And then I changed the hostname of my application in Config.groovy:
- grails.serverURL = "<a href="http://localhost/${appName}">http://localhost/${appName}" + grails.serverURL = "//localhost/${appName}" And I hard coded server port number in BuildConfig.groovy: + // Port configuration: + grails.server.port.http = "80"
+ grails.server.port.https = "443" Then, all my resource() are generated without "http:", and they still domain absolute, but now protocol relative. :D
Thanks to everybody! Cheers, Caio. On Thu, Oct 27, 2011 at 11:01 AM, [hidden email] <[hidden email]> wrote: I want to override resource() method, but since it isn't a plugin anymore, I have no idea how to do that decently (to be available in all files instead of only one). |
| Powered by Nabble | Edit this page |
