我们说我有以下域类:
第一个域类Tag.groovy
允许我构建具有不同层(layer1,layer2和layer3)的动态类别结构。 Activity.groovy
中的每项活动都属于某个类别,并通过ActivityTag.groovy
与其类别相关联。到现在为止还挺好。 :)
Tag.groovy
class Tag {
String name
String layer
Tag parent
static transients = ['activityCount', 'children']
int activityCount
static constraints = {
name nullable: false, unique: true
layer nullable: false, inList:['generic','layer1','layer2','layer3']
parent nullable: true
}
Set<Tag> getChildren() {
return Tag.findAllByParent(this) as Set
}
Set<Activity> getActivities() {
return ActivityTag.findAllByTag(this)*.activity
}
Set<Activity> getActivities(VirtualCity virtualCity) {
def activitiesWithTag = ActivityTag.findAllByTag(this)*.activity
return activitiesWithTag.findAll({it.virtualCity==virtualCity})
}
def getActivityCount(){
if (this.layer == "layer1") {
return this.children*.children*.activityCount.sum().sum()
} else if (this.layer == "layer2") {
return this.children*.activityCount.sum()
} else {
return this.getActivities().size()
}
}
def getActivityCount(VirtualCity virtualCity){
if (this.layer == "layer1") {
return this.children*.children*.activityCount.sum().sum()
} else if (this.layer == "layer2") {
return this.children*.activityCount.sum()
} else {
return this.getActivities(virtualCity).size()
}
}
}
Activity.groovy
class ActivityTag {console
Activity activity
Tag tag
static mapping = {
id composite: ['activity', 'tag']
version false
}
}
ActivityTag.groovy
class Activity {
VirtualCity virtualCity
String name
}
现在我想在树视图中将我的类别渲染为JSON。你能告诉我怎么做到这一点吗?
我尝试了render(template: "tree", collection: Tag.findAllByLayer("layer1"), var: 'tag')
使用此模板_tree.gson
:
model {
Tag tag
}
json {
id tag.id
name tag.name
activityCount tag?.activityCount ?: null
children g.render(template: 'tree', collection: tag?.children, var: 'tag') ?: null
}
但此方法因java.lang.reflect.InvocationTargetException: null