Grails webapp 不显示 gsp 页面

Grails webapp not displaying gsp page

我在显示 .gsp 文件时遇到一些问题,我不太确定原因。我有以下代码:

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

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

然后在 grails-app/views/index 我有 index.gsp:

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

当我点击 http://localhost:8080/ 时,我收到 500 状态代码错误。但是,如果我将 IndexController 更改为

render "Hello World" 

它将显示 "Hello World",因此该应用似乎正在启动。

有人知道这是怎么回事吗?部分堆栈跟踪:

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'

您收到的错误是因为 Grails 无法找到您的视图位置。

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

所以在您的情况下,当您点击 URL http://localhost:8080/ 时,您的 URLMapping 会将其重定向到您的控制器 index 操作,并且它会呈现相应的视图。

喜欢下面

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")   
    } 
}

然后在 grails-app/views/provision/ 中创建 index.gsp

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

您在错误的位置添加视图 grails-app/views/index.gsp 将其移至 grails-app/views/provision/index.gsp

Renamed your IndexController to ProvisionController in above example.