【问题标题】:Grails 4.0.2. Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'圣杯 4.0.2。无法解析名称为“grailsDispatcherServlet”的 servlet 中名称为“index”的视图
【发布时间】:2020-04-08 10:59:20
【问题描述】:

我使用以下代码创建了一个新的 grails 应用程序

grails create-app myapp --profile=rest-api

我修改了ApplicationController并添加了namespace,如下:

class ApplicationController implements PluginManagerAware {

     /** The Namespace for the version of the API, see http://docs.grails.org/latest/guide/REST.html#versioningResources */
     static namespace = "v1"

     GrailsApplication grailsApplication
     GrailsPluginManager pluginManager

     def index() {
         [grailsApplication: grailsApplication, pluginManager: pluginManager]
     } 
}

views/application/ 目录下存在一个index.gson 文件。

我使用grails run-app 运行此设置并将我的浏览器指向http://localhost:8080

它抛出以下异常:

<=======2020-04-08 16:01:31.616 ERROR --- [nio-8080-exec-1] .a.c.c.C.[.[.[.[grailsDispatcherServlet] : Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'] with root cause

但是,如果我修改控制器代码并使用 render,一切正常。

render(view:'index',model: [grailsApplication: grailsApplication, pluginManager: pluginManager])

grails -v 的输出 Grails 版本:4.0.2
JVM版本:1.8.0_171 操作系统:macOS High Sierra

【问题讨论】:

    标签: grails grails-plugin grails-4


    【解决方案1】:

    Selecting Views For Namespaced Controllers

    命名空间视图的正确路径应该是:

    grails-app/views/v1/application/index.gson
    

    【讨论】:

    • 谢谢@erichelgeson,我读了,最后一行写着If the view is not found in the namespaced directory then Grails will fallback to looking for the view in the non-namespaced directory. 同样当使用render 方法时,一切正常。 "
    • 哦,你是对的!然后我会打开一个问题,因为我希望它可以根据您提到的文档工作。
    • 谢谢@erichelgeson。目前我将使用渲染继续。再次感谢。
    • 这里是 repo 的链接,重现问题:github.com/devravi/grails_view_index_issue
    猜你喜欢
    • 2015-08-06
    • 2016-12-27
    • 2016-08-20
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多