1

我有一个类结构:

class A {
...
}

class B extends A {
...
}

class C extends A {
...
}

不,在控制器中,我正在获取混合类型的对象列表:

A[] objects = bethodTpFetchTheList()

在视图中,我需要渲染整个列表,但是我需要为不同的类型使用不同的模板。

甚至可能吗?

当我有单一类型时,我曾经以这种方式呈现 json:

json tmpl.object(objects)

有没有办法手动遍历列表并根据类型做出决定?

一些进展 所以我得到了这个:

json utilizations, { ToolUtilization utilization ->
    if (utilization.type == ToolType.TOOL_40_PRINCIPLES) {
        tmpl.'/fortyPrinciplesUtilization/utilization'(utilization)
    } else if (utilization.type == ToolType.RRM){
        tmpl.'/rrmUtilization/utilization'(utilization)
    }
}

它有点工作,但它呈现空对象......

更多进展

似乎如果我使用g.inline它部分工作,但它不会拾取模板。所以,如果我这样做:

json(utilizations) { ToolUtilization utilization ->
    if (utilization.type == ToolType.TOOL_40_PRINCIPLES) {
        g.inline(utilization) <= here it renders the object with a default renderer.
    } else if (utilization.type == ToolType.RRM){
        g.inline(template:'/rrmUtilization/utilization', model:[utilization: utilization])
    }
}

另一个定义了模板,产生一个空对象。

4

1 回答 1

-1

这真的取决于细节,但这可能会有所帮助。

理想的做法是将数据组织到控制器或服务层中的单独列表中,并使您的视图层更简单,但要回答所提出的问题,https://github.com/jeffbrown/renderjsonobjects上的项目显示了一个方法来做到这一点。

感兴趣的文件:

https://github.com/jeffbrown/renderjsonobjects/blob/master/grails-app/controllers/renderjsonobjects/DemoController.groovy

package renderjsonobjects

class DemoController {
    static responseFormats = ['json', 'xml']

    def index() {
        // the intent here is just to simulate a list of
        // instances of different types...
        def results = []
        results << new Person(name: 'Zack')
        results << new Address(town: 'St. Louis')
        results << new Person(name: 'Matt')
        results << new Address(town: 'San Jose')

        respond view: 'index', model: [theData: results]
    }
}

https://github.com/jeffbrown/renderjsonobjects/blob/master/grails-app/views/demo/_person.gson是一个用于渲染Person.

import renderjsonobjects.Person

model {
    Person person
}

json {
    name person.name
}

https://github.com/jeffbrown/renderjsonobjects/blob/master/grails-app/views/demo/_address.gson是一个用于渲染的模板Address

import renderjsonobjects.Address

model {
    Address address
}

json {
    town address.town
}

https://github.com/jeffbrown/renderjsonobjects/blob/master/grails-app/views/demo/index.gson迭代异构List并为不同类型呈现不同的模板。同样,这可能不是解决您真正问题的最佳解决方案,但这是实现目标的一种方式。

import renderjsonobjects.Address
import renderjsonobjects.Person

json {
    Map theModel = (Map)binding.variables.model
    List data = (List)theModel.theData
    people tmpl.person(data.findAll { it instanceof Person })
    addresses tmpl.address(data.findAll { it instanceof Address})
}

这将呈现如下内容:

$ curl http://localhost:8080/demo
{"people":[{"name":"Zack"},{"name":"Matt"}],"addresses":[{"town":"St. Louis"},{"town":"San Jose"}]}

根据评论更新:

请参阅https://github.com/jeffbrown/renderjsonobjects/commit/13aea5db090cd38a2039e08fb9b675630d5bf565

这使得https://github.com/jeffbrown/renderjsonobjects/blob/master/grails-app/views/demo/index.gson如下所示:

json (((Map)binding.variables.model).theData)

这导致呈现以下内容:

[[{"name":"Zack"},{"town":"St. Louis"},{"name":"Matt"},{"town":"San Jose"}]]

我认为这可以满足所提出的问题。如果您想看到 JSON 结构不同,如果您可以提供所需的输出,那将有所帮助。

于 2018-10-25T18:21:56.157 回答