EL 会自动转换/转换类型吗?${a.name} 实际上是如何工作的?

编程入门 行业动态 更新时间:2024-10-27 13:31:38
本文介绍了EL 会自动转换/转换类型吗?${a.name} 实际上是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个声明为 Object a 类型的变量,它实际上引用了 A 类型的实例.

I have a variable declared as type Object a which actually refers an instance of type A.

在EL中,我可以直接用下面的表达式打印A类型的name属性:

In EL, I can directly use the following expression to print the name property of type A:

${a.name}

它是如何工作的?

推荐答案

EL 使用 reflection 在幕后,通常通过 javax.beans.Introspector API.

EL uses reflection under the hoods, usually via javax.beans.Introspector API.

这就是它在 ${a.name} 上大致做的事情.

This is what it roughly does under the covers on ${a.name}.

// EL will breakdown the expression. String base = "a"; String property = "name"; // Then EL will find the object and getter and invoke it. Object object = pageContext.findAttribute(base); String getter = "get" + property.substring(0, 1).toUpperCase() + property.substring(1); Method method = object.getClass().getMethod(getter, new Class[0]); Object result = method.invoke(object); // Now EL will print it (only when not null). out.println(result);

它不会以任何方式转换/转换类型.

It does not convert/cast the type in any way.

  • 我们的 EL 维基页面
  • 如何访问 EL 表达式语言中的对象 ${}
  • 在 java 中遍历对象

更多推荐

EL 会自动转换/转换类型吗?${a.name} 实际上是如何工作的?

本文发布于:2023-10-13 14:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   工作   EL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!