在 Grails 中获取 bean 属性名称

发布于 2024-11-18 15:22:15 字数 272 浏览 4 评论 0原文

我正在尝试编写一个标签,它将呈现我的 bean 属性和相应的值。我希望默认行为是渲染 bean 中的所有属性。所以我需要以某种方式从传递的 bean 中获取所有属性名称。

我想我可以使用 properties 映射,但是尽管有 bean 属性,但还有其他东西,我必须手动管理它,这可能容易出错。

我还考虑过使用 DefaultGrailsDomainClass ,它对于域类很方便,但对于命令对象来说毫无用处。

你曾经做过类似的事情并想出一些有用的东西吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-11-25 15:22:15

就像这里所说,还有persistentProperties。但我相信你需要 GrailsDomainClass.properties - 不要与 Groovy properties 混淆,前者用于域

对于渲染, GrailsDomainClassProperty.naturalName 也很有用。

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.

捂风挽笑 2024-11-25 15:22:15

我已经使用 properties 完成了类似的事情,没问题。我的代码是:

value.properties.entrySet().each { Map.Entry it ->
    println "$it.key = $it.value"  
}

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"  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文