一次渲染多个对象

发布于 2024-11-29 04:30:40 字数 767 浏览 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 json = clientList as JSON
        log.debug("showClients :: jsondata = "+json)
        render json
    }else{
        redirect(controller:'login',action: "auth")
    }
}

这里我仅将 clientList 渲染为 json,但我还想渲染 point 对象。如何同时渲染 clientListpoint 对象?

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, but I also want to render the point object. How can I render both clientList and point object at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-12-06 04:30:40

您可以尝试:

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

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

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

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 */ }
}
幸福%小乖 2024-12-06 04:30:40

在渲染为 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")
    }
}

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")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文