Search code examples
grailsgsp

Grails webapp not displaying gsp page


I am having some issues displaying a .gsp file and I am not quite sure as to why. I have the following code:

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

class IndexController{
    def index(){
        render(view: "index")
    } 
}

And then in grails-app/views/index I have index.gsp:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Hello World
    </body>
</html>

When I hit http://localhost:8080/ I get a 500 status code error. If, however, I change the IndexController to have

render "Hello World" 

it will display "Hello World", so the app seems to be launching.

Does anyone know what's going on? Part of the stacktrace:

17:09:40.677 [http-nio-8080-exec-1] ERROR o.a.c.c.C.[.[.[.[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet'

Solution

  • The error you are getting is because of Grails is not able to find out the location of your view.

    Well avoid the names which have some predefined context in the framework(Just an suggestion not an problem in your case).

    As you have used the index for controller change it to something else

    So in your case when you will hit the URL http://localhost:8080/ your URLMapping will redirect it to your controllers index action and it will render the corresponding view.

    Like below

    class UrlMappings{
        static mappings = {
            "/"(controller: 'provision', action: 'index')
        }
    }
    
    class ProvisionController{
    
        def index(){ 
            // You don't really need to render it grails will render
            // it automatically as our view has same name as action
            render(view: "index")   
        } 
    }
    

    And then in grails-app/views/provision/ create index.gsp

    <!DOCTYPE html>
    <html>
        <head>
            <title>Hello World</title>
        </head>
        <body>
            Hello World
        </body>
    </html>
    

    You were adding the view at wrong location grails-app/views/index.gsp move it to grails-app/views/provision/index.gsp

    Renamed your IndexController to ProvisionController in above example.