带有hasMany的Grails类

时间:2013-06-18 16:26:26

标签: grails gorm grails-2.0 grails-domain-class grails-controller

我有这个具有一对多关系的域类,如下面的动态脚手架显示:

package mienapp

class Announcements {
    String user
    String title
    String comments
    Date dateCreated


    static hasMany = [tag: Tags]

    static mapping = {  
       comments sqlType: 'text'
    }

    static constraints = {
    }
}

控制器:

package mienapp
class AnnouncementsController {
  def scaffold = true
  def index() { 
     redirect(action: list)
  }
}

当控制器重定向到列表时,该表显示在公告类中定义的所有字段。如何在表格中显示标签的字段值?

1 个答案:

答案 0 :(得分:2)

假设您的list方法返回一个带有Announcements实例的模型

def list() {
  ..    
  [announcementsInstance: announcementsInstance, ...]
}

在您的视图中,您可以像这样访问标签

<g:each in="${announcementsInstance.tag}" var="tag">
  ${tag.someproperty}
</g:each>