0

I'm trying to write a tag, which will render my bean properties and corresponding values. I want the default behaviour be to render all properties from the bean. So I need somehow get all property names from passed bean.

I figured that I could use properties map, but despite bean properties, there are also other things and I'd have to manage it by hand which may be error prone.

I also thought of using DefaultGrailsDomainClass which is handy for domain classes, but is useless for command objects.

Have you ever done something similar and came up with something useful?

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
jjczopek
  • 3,319
  • 2
  • 32
  • 72
  • Have you looked at the bean-fields plugin? It sounds like you might be trying to re-implement some of it's functionality – Dónal Jul 04 '11 at 07:49
  • Yes I did. One thing that differs what I'm trying to do from bean-fields is that I want to render all properties at once, just by passing object to tag. In bean-fields, you have to explicitly tell which properties to render. – jjczopek Jul 04 '11 at 13:09

2 Answers2

2

Like said here, there are also persistentProperties. But I believe you need GrailsDomainClass.properties - don't confuse with Groovy properties, the former are for domain class.

For rendering, GrailsDomainClassProperty.naturalName will also be useful.

Community
  • 1
  • 1
Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
  • So for now I'm going to narrow functionality and allow only bean rendering and use GrailsDomainClass. Maybe some time I'll try to make it available for command objects. – jjczopek Jul 03 '11 at 18:14
  • Command is not a bean BTW - I learned this lately. Maybe you can just use `Class.declaredFields/declaredMethods` for them. – Victor Sergienko Jul 03 '11 at 20:35
0

I've done similar thing by using properties, no problem. My code was:

value.properties.entrySet().each { Map.Entry it ->
    println "$it.key = $it.value"  
}
Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
  • I'm aware it is possible, but I in such scenario I have to omit by hand such entries like metaClass, transients, class and few more. Also, if there are relationships involved, I have to manage it by hand. – jjczopek Jul 03 '11 at 14:23
  • Oh, I got it. At my case it wasn't required – Igor Artamonov Jul 03 '11 at 14:39