Grails/Groovy - 域对象 - 其属性映射 [英] Grails / Groovy - Domain Object - Map of its Properties
本文介绍了Grails/Groovy - 域对象 - 其属性映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何才能获得我的一个域对象上仅用户定义属性的键/值的映射?
How can I get a map of the key/values of only the user-defined properties on one of my domain objects?
问题是如果我自己做这件事,我会得到我的属性加上类、元类、约束、闭包等......
Problem is if I do this myself, I get my properties plus class, metaClass, constraints, closures, etc...
我认为 Grails 可以很容易地做到这一点,因为它是在脚手架代码的某个级别完成的,对吗?我自己怎么做?
I assume Grails can do this fairly easily because it is done at some level in the scaffold code right? How can I do this myself?
推荐答案
试试这个
class Person{
String name
String address
}
def filtered = ['class', 'active', 'metaClass']
def alex = new Person(name:'alex', address:'my home')
def props = alex.properties.collect{it}.findAll{!filtered.contains(it.key)}
props.each{
println it
}
如果您使用 alex.metaClass.surname = 'such'
,它也可以使用.此属性将显示在每个循环中
It also works if you use alex.metaClass.surname = 'such'
. This property will be displayed in the each loop
这篇关于Grails/Groovy - 域对象 - 其属性映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文