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.

5 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. :)

Jonathan's Memory Lack said...

That's help me greatly !
See on Stackoverflow