【问题标题】:Grails webapp not displaying gsp pageGrails webapp不显示gsp页面
【发布时间】:2016-09-29 16:12:27
【问题描述】:

我在显示 .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 clean 并重新启动 Grails 运行时即可。
  • 避免在框架中使用具有特定含义的名称。如果将 Index 更改为其他内容,是否会出现相同的错误?
  • 也不会http://host/index/index 看起来有点不对劲吗?无论如何 "/index"(controller: 'aha', action:"nice" ) 怎么样,然后将/index 重定向到其他一些控制器操作,您也可以为/index/index 编写它,但认为它看起来有点奇怪人们开始质疑开发人员的技能:)

标签: grails gsp


【解决方案1】:

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

避免在框架中具有一些预定义上下文的名称(只是一个建议,在您的情况下不是问题)。

由于您已将 index 用于控制器,请将其更改为其他内容

因此,当您点击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

在上面的示例中将您的 IndexController 重命名为 ProvisionController

【讨论】:

  • 您好 Prakash,感谢您的回答。我设法弄清楚它为什么会这样。我不相信我把它放在了错误的位置,因为我在 grails-app/views/index/ 中有 index.gsp。我认为它不起作用的原因是因为在 build.gradle 我设置了配置文件“org.grails.profiles:rest-api”而不是 :web (当我第一次开始我的小项目时,我只对 API 功能感兴趣,并且不担心前端)
猜你喜欢
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多