Sunday, January 4, 2009

Grails tips for homepage url mapping

Hi anyone who's reading

Heres a little undocumented tip for grails. It is only a small thing and very easy to solve in grails (once you know how) but none the less it did give me some trouble but only in grails years (which are 100 times less than other frameworks ;) )

Anyway i needed to create link to my homepage on my new site. Now this is default page the root or root context (if you have a root context).

eg in development it would be where as in production it would be

So how do you get grails to create a link for you. You can't just use ${createLink(url:'/')} as that is not contextual to the context and so would not work in development as this is harded coded url. Plus you do not have any default controller to link too. So what to do? Well it turns out this very easily solved by creating new controller and urlmapping combination.

Create a new Home Controller:

class HomeController {

def index = {}

Then in your grails-app/views/home directory copy your index.gsp

Then create a urlmapping to go with it that links the controller and action to the root url "/".

class UrlMappings {
static mappings = {


constraints {
// apply constraints here


Then finally to create a link to the controller simple do this:

<a href="${createLink(controller:'home',action:'index')}">Home</a>

Hope this helps someone :)


nic said...

That's pretty schweet. Thanks!

TheAa said...

this works for me...

<a class="home" href="${createLinkTo(dir:'')}">

Anonymous said...

Nice post! Much better than having a single index.gsp page with no controller

Anonymous said...

really helpful..wanted to do exactly same and has same trouble.

Anonymous said...

In the grails code I used the following (which considers the context path):


Tim said...

This is a great tip because it also lets you load in domain objects in the controller to be passed to the view of the home page. Assuming the site being built actually does anything at all then you will need this, as opposed to a static / to index view mapping that comes out of the box. Thanks Mr D. Tim Stokes

ceaseoleo said...

using grails 1.2 when I go to the root url, this method doesn't work. example I'm going to http://localhost:8080/name and i get an apache error. Wondering if you can verify ?

Peter said...

Hello everybody,
Did not think I would spend another couple of hours on this. In 1.2 it does not work for some reason.

I created the following tag lib:

class ApplicationPathTagLib {

//static namespace = 'applicationPath'
* @return application path
def applicationPath = {attrs->
final String contextPath = request.getContextPath();

final String protocol = request.getScheme();
final String domain = request.getServerName();
final int serverPort = request.getServerPort();
final String port = Integer.toString(serverPort);

final StringBuffer requestURLStringBuffer = new StringBuffer();
requestURLStringBuffer.append("://"); //$NON-NLS-1$
final String hostPath = requestURLStringBuffer.toString();

String applicationPath = hostPath;
if (!contextPath.isEmpty())
applicationPath = hostPath.substring(0, hostPath
+ contextPath.length());

// System.out.println("requestURL:" + hostPath);
// System.out.println("contextPath:" + contextPath);
// System.out.println("applicationPath:" + applicationPath);
out << applicationPath


and used it in GSP's like this:

<a href="${g.applicationPath()}">
Yo, home, man.

Seems to work for me. Hope that helps somebody.

Yuri said...

createLinkTo is deprecated. Use:

Both the taglib and the controller are not necessary.

David Arturo Cruz Sánchez said...

Thank's a lot, works for me, I'm using grails 1.3.3

FitzChivalry said...
This comment has been removed by the author.
Buzz the Windling said...

I believe the apache error is because Grails is looking for the index.gsp to be in the views/home directory. Once I moved my index.jsp to this directory, it worked on grails 1.3.6

Anonymous said...

Thank you, it worked for me too!

Anonymous said...

You are the MAN!!! I have been trying to get a POST to work with Grails Spring Security plugin. It would never get a 'j_username' parameter from my POST. I converted your code to .coffee and it worked immediately. THANK YOU SO MUCH FOR THIS!