Java 中的 OGNL Hello World
我需要使用 OGNL 从 Java 对象读取一些属性。 OGNL 对我来说是全新的事物。 OGNL 的可用文档是 OGNL 的网站,这对我来说真的很困惑。
因此任何人都可以提供一个使用 OGNL 的简单 HelloWorld
示例(或者任何指向教程的链接也很有帮助)。
I need to use OGNL for reading some properties from Java object. OGNL is completely new thing to me. The documentation available for OGNL is OGNL's website is really confusing to me.
So anyone can provide a simple HelloWorld
example for using OGNL (or any link to a tutorial is also helpful).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这个:
Try this:
如果目的只是从对象中读取属性,那么 PropertyUtils.getProperty (来自 commons-beanutils)可能就足够了。 然而,如果目的是评估条件等,那么 Ognl 可能会受益。
这是带有布尔值的相同 Dimension 示例:
If the intention is only to read properties from an object then PropertyUtils.getProperty (from commons-beanutils) may suffice. However, if the intention is to evaluate conditionals and such, then Ognl may benefit.
Here is the same Dimension example with a boolean:
OGNL 允许您通过字符串表达式访问对象字段和方法,当您失去数据与其使用者之间的耦合架构时,这会变得非常有用。 它在底层使用反射,但与纯粹的反射方法相比,肯定会加快开发速度。
一些一行示例
文档已经有一些基本的和更多的内容高级 ognl 表达式。
OGNL allows you to access objects fields and methods via string expressions which becomes very useful when you have lose coupled architecture between data and it's consumers. It's using reflection under the hood but definitely speeds up development compared to a pure reflection approach.
Some one line examples
Documentation already has a number of basic and more advanced ognl expressions.
下面是 jython (编译为 java 的 python)的 helloworld 示例。
Here is an example helloworld for jython (python that compiles to java).