Wednesday, May 14, 2008

Redirecting from index.gsp

To redirect to an initial page from the index.gsp

1. Create a TagLib file
  grails-app\taglib\MyAppTagLib.groovy

2. Define a method that routes to your first page
  class MyAppTagLib {
    def redirectMainPage = {
      response.sendRedirect("${request.contextPath}/myController/welcomePage/")
    }
  }


3. Change your index.gsp to contain only:
  <g:redirectMainPage/>

Note: One advantage to this approach is that you have a coding opportunity over simply mapping the "/" url.

For example, if you want to provide features or data based on a specific user's login id.

6 comments:

kouphax said...

Am I missing something? That contains only what...?

kouphax said...

Oh blogger has stripped your tags I assume :-P

Yeah I find it strange that something this simple isn't available already in a tag. JSP tags are on the road map from what I gather so perhaps that's why this wasn't re-implemented.

James.

Unknown said...

Why not map the / URL to your controller/action?

[]s,

Lucas

Unknown said...

Thanks for catching the mistake, thats what I get for not reviewing it ... too hasty. :)

Brandon Franklin said...

Actually, wouldn't it be a lot better to just embed your redirect straight into the GSP itself, like described here:
http://www.mcdonaldland.info/2008/04/24/grails-redirect/

I don't see why you'd want to create a TagLib just for this simple purpose. Also note in the comment on that blog post that it's also better to let Grails generate the full path for you with createLink.

Jonathan's Memory Lack said...

That's help me greatly !
See on Stackoverflow