Ed.T
Ed.T

Reputation: 1705

How do you change the default homepage in a Grails application?

What is the configuration setting for modifying the default homepage in a Grails application to no longer be appName/index.gsp? Of course you can set that page to be a redirect but there must be a better way.

Upvotes: 37

Views: 30379

Answers (7)

John Little
John Little

Reputation: 12447

If anyone is looking for the answer for gails 3.x, they moved UrlMappings.groovy to grails-app/controllers/appname

As the below answers say, just ed it the line starting with "/".

In my case its:

"/"(controller:"dashboard", view:"/index")

Upvotes: 0

Robert Erlinger
Robert Erlinger

Reputation: 321

Use controller, view and action parameter by the following syntax:

class UrlMappings {
   static mappings = {
       "/" (controller:'dashboard', view: 'index', action: 'index')
       "500"(view:'/error')
   }
}

Upvotes: 5

Mehul Katpara
Mehul Katpara

Reputation: 1761

All the answers are correct! But let's imagine a scenario:

I mapped path "/" with the controller: "Home" and action: "index", so when i access "/app-name/" the controller Home gets executed, but if i type the path "/app-name/home/index", it will still be executed! so there are 2 paths for one resources. it would work until some one finds out "home/index" path.

another thing is if I have a form without any action attribute specified, so by default it will be POST to the same controller and action! so if the form is mapped to "/" path and no action attribute is specified then it will be submitted to the same controller, but this time the path will be "home/index" in your address-bar, not "/", because it's being submitted to the controller/action not to the URI.

To solve this problem what you have to do, is to remove or comment out these lines.

//        "/$controller/$action?/$id?(.$format)?"{
//            constraints {
//                // apply constraints here
//            }
//        }

So now when you access "/", will work. but "home/index" will not. But there's a one flaw, now you have to map all the paths to the controllers manually by explicitly writing into URLMapping file. I guess this would help!

Upvotes: 0

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

You may try as follows
in the UrlMappings.groovy class which is inside the configuration folder:

class UrlMappings {

    static mappings = {

        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        //"/"(view:"/index")
        "/" ( controller:'Item', action:'index' ) // Here i have changed the desired   action to show the desired page while running the application
        "500"(view:'/error')
    }
}

hope this helps,
Rubel

Upvotes: 14

Bob Herrmann
Bob Herrmann

Reputation: 9938

Add this in UrlMappings.groovy

 "/" {
    controller = "yourController"
    action = "yourAction"
 }

By configuring the URLMappings this way, the home-page of the app will be yourWebApp/yourController/yourAction.

(cut/pasted from IntelliGrape Blog)

Upvotes: 60

element40
element40

Reputation: 45

Simple and Neat

  1. Go to File: grails-app/conf/UrlMappings.groovy.

  2. Replace the line : "/"(view:"/index") with "/"(controller:'home', action:"/index").

Home is Your Controller to Run(Like in spring security you can use 'login' ) and action is the grails view page associated with your controller(In Spring Security '/auth').

Add redirection of pages as per your application needs.

Upvotes: 2

dahernan
dahernan

Reputation: 3143

Edit UrlMappings.groovy

Add for example add this rule, to handle the root with a HomeController.

"/"(controller:'home')

Upvotes: 13

Related Questions