一次渲染多个对象

时间:2011-08-13 11:49:28

标签: grails groovy gsp

我正在尝试将多个对象渲染为JSON。我的控制器代码如下:

 def showClient = {
    if (springSecurityService.isLoggedIn()) {
        def q_param = params.name_startsWith;
        def listOfClients =ClientRole.findAll("FROM ClientRole WHERE  party.name LIKE ? AND is_active =true",["%"+q_param+"%"])
        def point= Point.get(1)

        ArrayList<DisplayableName> clientList = ParameterFormatter.getFormattedDisplayNameList(listOfClients)
        def json = clientList as JSON
        log.debug("showClients :: jsondata = "+json)
        render json
    }else{
        redirect(controller:'login',action: "auth")
    }
}

这里我只渲染clientList作为json,但我也想呈现point对象。如何同时呈现clientListpoint对象?

2 个答案:

答案 0 :(得分:3)

您可以尝试:

render(contentType: 'text/json') {
    delegate.clientList = clientList
    delegate.point = point
}

这将产生一个JSON对象,如:

{
    "clientList": [ /* client list */ ],
    "point": { /* point object */ }
}

答案 1 :(得分:0)

在渲染为JSON之前,我总是将所有内容都放在地图中!所以试试这个:

def showClient = {
    if (springSecurityService.isLoggedIn()) {
        def q_param = params.name_startsWith;
        def listOfClients =ClientRole.findAll("FROM ClientRole WHERE  party.name LIKE ? AND is_active =true",["%"+q_param+"%"])
        def point= Point.get(1)

        ArrayList<DisplayableName> clientList = ParameterFormatter.getFormattedDisplayNameList(listOfClients)
        def map = [clients:clientList]
        map << [point:point]
        def json = map as JSON
        render json


    }else{
        redirect(controller:'login',action: "auth")
    }
}