Java 中的 OGNL Hello World

发布于 2024-07-19 02:39:42 字数 168 浏览 6 评论 0原文

我需要使用 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 技术交流群。

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

发布评论

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

评论(4

时间海 2024-07-26 02:39:42

尝试这个:

    Dimension d = new Dimension(2,2);

    String expressionString = "width";
    Object expr = Ognl.parseExpression(expressionString);

    OgnlContext ctx = new OgnlContext();
    Object value = Ognl.getValue(expr, ctx, d);

    System.out.println("Value: " + value);

Try this:

    Dimension d = new Dimension(2,2);

    String expressionString = "width";
    Object expr = Ognl.parseExpression(expressionString);

    OgnlContext ctx = new OgnlContext();
    Object value = Ognl.getValue(expr, ctx, d);

    System.out.println("Value: " + value);
丑疤怪 2024-07-26 02:39:42

如果目的只是从对象中读取属性,那么 PropertyUtils.getProperty (来自 commons-beanutils)可能就足够了。 然而,如果目的是评估条件等,那么 Ognl 可能会受益。

这是带有布尔值的相同 Dimension 示例:

Dimension d = new Dimension();
d.setSize(100,200) ;// width and height

Map<String,Object> map = new HashMap<String,Object>();
map.put("dimension", d);

String expression = "dimension.width == 100 && dimension.height == 200";
Object exp = Ognl.parseExpression(expression);
Boolean b = (Boolean) Ognl.getValue(exp,map);
// b would evaluate to true in this case

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:

Dimension d = new Dimension();
d.setSize(100,200) ;// width and height

Map<String,Object> map = new HashMap<String,Object>();
map.put("dimension", d);

String expression = "dimension.width == 100 && dimension.height == 200";
Object exp = Ognl.parseExpression(expression);
Boolean b = (Boolean) Ognl.getValue(exp,map);
// b would evaluate to true in this case
孤单情人 2024-07-26 02:39:42

OGNL 允许您通过字符串表达式访问对象字段和方法,当您失去数据与其使用者之间的耦合架构时,这会变得非常有用。 它在底层使用反射,但与纯粹的反射方法相比,肯定会加快开发速度。

一些一行示例

System.out.println(Ognl.getValue("x", new Point(5,5)));
System.out.println(Ognl.getValue("size", new ArrayList<Object>()));

文档已经有一些基本的和更多的内容高级 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

System.out.println(Ognl.getValue("x", new Point(5,5)));
System.out.println(Ognl.getValue("size", new ArrayList<Object>()));

Documentation already has a number of basic and more advanced ognl expressions.

|煩躁 2024-07-26 02:39:42

下面是 jython (编译为 java 的 python)的 helloworld 示例。

from ognl import Ognl, OgnlContext
from java.lang import String

exp = Ognl.parseExpression("substring(2, 5)")

print Ognl.getValue(exp, OgnlContext(), String("abcdefghj"))

Here is an example helloworld for jython (python that compiles to java).

from ognl import Ognl, OgnlContext
from java.lang import String

exp = Ognl.parseExpression("substring(2, 5)")

print Ognl.getValue(exp, OgnlContext(), String("abcdefghj"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文