开发者

Rendering more than one object at a time

开发者 https://www.devze.com 2023-03-28 09:16 出处:网络
I am trying to render multiple objects as JSON. My controller code looks like: def showClient = { if (springSecurityService.isLoggedIn()) {

I am trying to render multiple objects as JSON. My controller code looks like:

 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")
    }
}

Here I am rendering only clientList as json, bu开发者_Python百科t I also want to render the point object. How can I render both clientList and point object at the same time?


You can try:

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

This will result in a JSON object like:

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


I always put everything in a map before rendering as JSON! so try this:

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")
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消