我正在使用Graisl 3.1.1,rest-api配置文件。 我想建立一个类别树,但我没有在JSON视图中呈现类别的一些问题。Grails 3.1 JSON-视图,呈现类别树
我正在使用json模板,一个用于父级,另一个用于子级。 基本上我想生成角的东西JSON这样的:
这是我的代码。
任何帮助?
//域
class Category {
ObjectId id /* MongoDB */
static hasMany = [categories: Category]
String name
...
//控制器
def directory(){
def categories = Category.findAllByCategoriesIsNotNull([sort: 'name', order: 'asc'])
respond categories
}
//directory.gson
import com.example.Category
model {
Iterable<Category> categoryList
}
json {
categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category')
}
//_parent.gson
import com.example.Category
model {
Category category
}
json {
id category.id.toString()
name category.name
categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child')
}
问题是上面的categories
行,我不确定是什么问题或我的错误。
//_child.gson
import com.example.Category
model {
Category child
}
json {
name child.name
}
我也更新了编译“org.grails.plugins:views-json:1.0.3”来编译“org.grails.plugins:views-json:1.0 .4“谢谢。 –