ibuck
ibuck

Reputation: 495

grails app root context

I have a test grails app setup with a context of "/testapp". When I add a link in my gsp that references / it does not go to the root of my grails.app.context, but to the root of my grails.serverURL property.

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

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

Is there a way that I can get references to / to start at my grails.app.context vs the grails.serverURL?

Upvotes: 8

Views: 12819

Answers (5)

user553180
user553180

Reputation: 626

I had a similar issue to OP - how to have grails form links that start at the context root and NOT server root?

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

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

will prefix any context if applicable, and produce 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/

Not sure why it's an undocumented attribute in the reference docs, but I found it in the Javadocs - ApplicationTagLib

Upvotes: 6

Matthias Hryniszak
Matthias Hryniszak

Reputation: 3162

And when it comes to elements like stylesheets I'd recommend creating a simple tag that'll do the trick, something along those lines:

class StylesTagLib {
    static namespace = "g"

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

and later on in your code use it like this:

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

Obviously you can fiddle with the conventions (should I use a predefined folder? should I add the .css extension automatically? stuff like that) but the general idea is to hide the ugliness behind a nicely defined tag.

Upvotes: 0

tmarthal
tmarthal

Reputation: 1528

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

${resource(uri:'/')}

then just use that string wherever.

Upvotes: 1

Aaron Saunders
Aaron Saunders

Reputation: 33345

use the request contextPath value on the page

${request.contextPath}

and then prepend the additional host information if necessary to construct the complete url

Upvotes: 22

hvgotcodes
hvgotcodes

Reputation: 120318

the question is how do you add your links into your gsps?

We do things like

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

and

<g:javascript library="prototype"/>

by using the g:javascript and resource tags and methods, you tell grails to set the path for you...

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

goto

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

and, under tags in the left hand nav, look for resource and/or javascript to get an idea (its difficult to link directly in to the docs...:()

Upvotes: 6

Related Questions