Grails JsonViews:使用响应从操作传递模型变量不会在gson视图中填充模型

时间:2019-01-18 10:08:46

标签: json grails controller

在JsonViews和模板的泥潭以及控制器的响应方法中苦苦挣扎。

环境:Grails 3.3.9 / json视图1.2.10

为此测试,我设置了URL映射,就像这样

get "/api/test2/$id"(controller:"test2", action:"thing")

然后在我的控制器中,该动作以设备对象d进行响应,并提供一个类似这样的模型图

//test want this to call views/test2/thing.gson view
//i.e default is for controller method name to map to <methodname>.gson view
def thing (Device device) {

    if(device == null) {
        render status:404
    } else {
        Device d = device
        respond d, model:[devCount:1, thing: "will"]
    }
}

发布变量devCount,并将事物作为模型变量

在thing.gson视图文件中,我添加了devCount和Thing作为模型变量

//model receives the object returned from controller action
model {
    Integer devCount
    String thing
    Device device
}
json.will {
    hello "hello from thing.gson view", "some more", "and some more"
    world  'this world'
    deviceName device.name
    numberOfDevices devCount
    name thing
}

但是当在浏览器中呈现时,devCount和thing都呈现为空

{"will":{"hello":["hello from thing.gson view","some more","and some more"],"world":"this world","deviceName":"ACME-HO-WAN1","numberOfDevices":null,"name":null}}

为什么发布的要响应的模型没有映射到同名的视图模型变量中?

0 个答案:

没有答案