本文介绍了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} 实际上是如何工作的?
发布评论