grails - Grails WebApp不显示GSP页面

标签 grails gsp

我在显示.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无法找到您的 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



因此,在您遇到URL http://localhost:8080/的情况下,您的URLMapping会将其重定向到 Controller 的index操作,并将呈现相应的 View 。

像下面
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>

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

Renamed your IndexController to ProvisionController in above example.

关于grails - Grails WebApp不显示GSP页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39775240/

相关文章:

spring - 如何在 Grails 中不断检查上传大小?

sql - 如何从数据库 grails 重新加载域类的实例?

grails - 数据截断 : Data too long for column grails GSP field

java - gsp 链接将命名参数和映射放在同一 params 属性中

grails - 在gsp中设置选择值

grails - Groovy 列表根据两个标准排序

mysql - 不在数据库中插入数据 - grails

grails - Grails中的脚手架出现404错误

email - 使用布局渲染 gsp/view 不起作用

html - 如何在<g:select>或<select>标记中显示也是 map 的 map 的值?