1

我正在使用 Graisl 3.1.1,rest-api 配置文件。我正在尝试构建类别树,但在 JSON 视图中呈现类别时遇到了一些问题。

我正在使用 json 模板,一个用于父级,另一个用于子级。基本上我想为这样的角度生成一个json:

angularjs 类别树

这是我的代码。

有什么帮助吗?

堆栈跟踪

//领域

class Category {
  ObjectId id /* MongoDB */
  static hasMany = [categories: Category]
  String name
  ...

//控制器

def directory(){
   def categories = Category.findAllByCategoriesIsNotNull([sort: 'name', order: 'asc'])
   respond categories
}

//目录.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
}
4

2 回答 2

1

我有理由相信您会遇到几周前遇到的相同的新错误(已在 grails-views@1.0.4 中修复)#9720

似乎 g.render 无法识别相对路径,并且即使模板与调用 gson 文件位于同一目录中,也需要完全限定的路径。

代替:

categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child')

在模板前面加上一个斜线:

categories g.render(template: "/category/child", collection: category.categories ?: [], var: 'child')

您可能还需要更改:

categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category')

至:

categories g.render(template: '/category/parent', collection: categoryList ?: [], var: 'category')
于 2016-03-08T23:48:33.693 回答
-1

我通常在控制器中使用

导入 grails.converters.JSON

接着

将结果呈现为 JSON

于 2016-03-02T12:48:39.463 回答