【问题标题】:Grails 3.1 JSON-Views, rendering a Category TreeGrails 3.1 JSON-Views,呈现类别树
【发布时间】:2016-06-14 19:30:24
【问题描述】:

我正在使用 Graisl 3.1.1,rest-api 配置文件。 我正在尝试构建类别树,但在 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
}

【问题讨论】:

    标签: angularjs mongodb gson grails-3.1 json-view


    【解决方案1】:

    我通常在控制器中使用

    导入 grails.converters.JSON

    然后

    将结果呈现为 JSON

    【讨论】:

    【解决方案2】:

    我有理由相信您会遇到与我几周前遇到的相同的新错误(在 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')
    

    【讨论】:

    • 我还更新了 compile "org.grails.plugins:views-json:1.0.3" 来编译 "org.grails.plugins:views-json:1.0.4" 谢谢。
    猜你喜欢
    • 2014-08-11
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多