使用Grails的gson视图并返回null

时间:2017-02-28 12:06:32

标签: grails gson

我有以下服务:

def getSectorByName(String sectorName) {

    Sector theSector = Sector.where {
        name == sectorName
    }.get()

    return theSector
}

我的控制器:

def getSector(String sectorName) {
    def theSector = sectorDataService.getSectorByName(sectorName)

    respond theSector
}

我的gson视图名为getSector:

import application.Sector

model {

    Sector theSector
}

json {
    theSector g.render(theSector)
}

这给了我以下结果:

{"theSector":null}

调试应用程序时,我可以看到theSector确实包含一个扇区,但这不会传递给视图。

1 个答案:

答案 0 :(得分:1)

由于您没有为扇区对象指定名称,因此默认的命名约定是使用" propertyName"班级

您的观点应该是:

import application.Sector

model {

    Sector sector
}

json {
    theSector g.render(sector)
}

请参阅有关调试视图的文档中的部分,以防止出现以下问题:http://views.grails.org/latest/#_debugging_views