找不到名称的 Grails 3.2.0.M1 模板

Grails 3.2.0.M1 Template not found for name

在我的域中 class com.example.users.User 我添加了临时字段通行证:

class User implements Serializable {
    ...
    def carnets

    static transients = ['springSecurityService', 'carnets']
    ...
}

在我的 gson 视图中 user/_user.gson 我想渲染它:

import com.example.users.User

model {
    User user
}

json g.render(user, [excludes:['password', 'deleted', 'enabled', 'accountExpired', 'accountLocked', 'passwordExpired', 'authorities']]) {
    //"carnets" g.render(template:"/carnet/index", collection: user.carnets, var:'carnets')
    "carnets" tmpl.'/carnet/index'(user.carnets)
}

但我收到了:

Caused by: grails.views.ViewRenderException: Error rendering view: Template not found for name /carnet/index

Carnet 的视图 gson 文件是自动生成的,并且在从 CarnetController 执行时工作正常。

我错过了什么?

不幸的是,您需要使用当前 .gson 文件中模板的相对路径。我想该选项将是 views 文件夹的相对路径,但显然不是这样。

假设您的 views 文件夹结构如下:

views/
    carnet/
         _index.gson
    user/
        _user.gson

在这种情况下,您的 _user.gson 文件需要如下所示:

import com.example.users.User

model {
    User user
}

json g.render(user, [excludes:['password', 'deleted', 'enabled', 'accountExpired', 'accountLocked', 'passwordExpired', 'authorities']]) {
    "carnets" tmpl.'../carnet/index'(user.carnets)
}

TL;DR 将 tmpl.'/carnet/index' 更改为 tmpl.'../carnet/index'。 :)

在我的用例 (Grails 3.3.0) 中,我必须将模板路径从: tmpl.'message/message' 到: tmpl.'/message/message' (添加了前导斜杠)。

使用 ../ 语法在开发中有效,但在将 WAR 文件部署到 Tomcat 时对我造成了错误。参见:[https://github.com/grails/grails-views/issues/140]