0
votes

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 "/"?

1
Did you remove the default mapping which maps "/" with "/"(view:"/index")?Jeff Scott Brown
It is very odd that even after deleting the files they are being rendered. There is some other piece of the puzzle that isn't mentioned in the question I expect.Jeff Scott Brown
Look for plugins contributing items that are getting in the way. If you can share a project which demonstrates the behavior, it would be more simple to troubleshoot.Jeff Scott Brown
Thanks @JeffScottBrown I think the view is scaffold autogenerated view that lists the controllers in my app. I think it might be some plugin since my code looks pretty much the same es your sample project. Thanks!Pablo Pazos
I am glad that you were able to chase it down. Well done!Jeff Scott Brown

1 Answers

1
votes

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.