grailsgrails3

How to map an action in grails 3 to "/"?


I'm using Grails 3.3.10 and trying to map "/" to a controller action, where the user is redirected after a successful login.

In UrlMappings I have:

class UrlMappings {

   static mappings = {

      "/"(controller:'app')
   ...
...

When the user signs in, the app is redirected to the root http://localhost:8090/, but it shows a generated view from Grails:

Welcome to Grails

Congratulations, you have successfully started your first Grails
application! At the moment this is the default page, feel free to
modify it to either redirect to a controller or display whatever
content you may choose. Below is a list of controllers that are
currently deployed in this application, click on each to execute
its default action:

Available Controllers:

....

I deleted the default index.gsp and the main.gsp layout, but that view with the controllers is still appearing and I can't get my action to be executed.

If I remove the UrlMapping "/"(controller:'app'), the action is executed OK and the view is the correct one, but the URL is http://localhost:8090/app/index

Is it possible to display the view from app/index being the URL mapped to "/"?


Solution

  • Is it possible to display the view from app/index being the URL mapped to "/"?

    Yes it is. See the project at https://github.com/jeffbrown/pablopazosurlmapping.

    https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/controllers/pablopazosurlmapping/UrlMappings.groovy

    package pablopazosurlmapping
    
    class UrlMappings {
    
        static mappings = {
            "/$controller/$action?/$id?(.$format)?"{
                constraints {
                    // apply constraints here
                }
            }
    
            "/"(controller:'app')
            "500"(view:'/error')
            "404"(view:'/notFound')
        }
    }
    

    https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/controllers/pablopazosurlmapping/AppController.groovy

    package pablopazosurlmapping
    
    class AppController {
        def index() {
            [name: 'Pablo']
        }
    }
    

    https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/views/app/index.gsp

    <%@ page contentType="text/html;charset=UTF-8" %>
    <html>
    <head>
        <title>Demo</title>
    </head>
    
    <body>
    <h2>${name} Was Here!</h2>
    </body>
    </html>
    

    I hope that helps.