Grails / Groovy - 域对象 - 其属性的映射 [英] Grails / Groovy - Domain Object - Map of its Properties

查看:133
本文介绍了Grails / Groovy - 域对象 - 其属性的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取我的域对象之间的用户定义属性的键/值的映射?

How can I get a map of the key/values of only the user-defined properties on one of my domain objects?

问题是如果我自己这样做我得到我的属性加类,metaClass,约束,闭包等...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆