Round application root context

I have a grails application installation with context "/ testapp". When I add a link to my gsp, the link to it / does not get to the root of my grails.app.context, but to the root of my grails.serverURL property.

For example, if a link is given with href "/css/main.css"

I would expect this link to really look in localhost: 8080 / testapp / css / main.css instead of localhost: 8080 / css / main.css

Is there any way I can get links to / to start with my grails.app.context vs grails.serverURL?

+6
grails groovy
source share
5 answers

The question is, how do you add your links to your gsps?

We do things like

<link rel="stylesheet" href="${resource(dir: 'css', file: 'stylesheet1.css')}"/> 

and

 <g:javascript library="prototype"/> 

using the g: javascript and resource tags and methods, you specify grails to indicate the path for you ...

I suspect you are just putting standard tags in ...

goto

http://grails.org/doc/latest/

and under the tags in the left hand, find the resource and / or javascript to get an idea (it’s difficult to connect directly with documents ... :()

+6
source share

use the request request context value on the page

 ${request.contextPath} 

and then add additional host information if necessary to build the full URL

+19
source share

I had a similar problem with OP - how to get grails from links that start with context root and not root root?

You can do this using the uri attribute for the g: link and g: createLink tags. For example:

 <g:link uri="/login">login</g:link> 

there will be a prefix of any context, if applicable, and will create the following

 <a href="/login">login</a> if your app is at the http://server/ <a href="/testapp/login">login</a> if your app is at http://server/testapp/ 

I don't know why this attribute is undocumented in referenced documents, but I found it in Javadocs - ApplicationTagLib

+6
source share

You should probably use the resource tag in your CSS grails directory as above. However, you can also use the resource method to find the root context of your web application using the same tag:

 ${resource(uri:'/')} 

then just use this line everywhere.

+1
source share

And when it comes to elements such as style sheets, I would recommend creating a simple tag that will do the trick, something like these lines:

 class StylesTagLib { static namespace = "g" def stylesheet = { args, body -> out << """<link rel="stylesheet" href="${resource(dir: 'css', file: args.href)}"/>""" } } 

and then in your code use it like this:

 <g:stylesheet href="main.css"/> 

Obviously, you can play with conventions (should I use a predefined folder? Should I automatically add the .css extension?), But the general idea is to hide the ugliness behind a beautifully defined tag.

0
source share

All Articles