EL是否会自动转换/转换类型? $ {a.name}实际如何工作? [英] Does EL automatically convert/cast the type? How does ${a.name} actually work?
本文介绍了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使用反省,通常通过 javax.beans.Introspector
API .
EL uses reflection under the hoods, usually via javax.beans.Introspector
API.
这是${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.
- Our EL wiki page
- How to access objects in EL expression language ${}
- Travesring through an object in java
这篇关于EL是否会自动转换/转换类型? $ {a.name}实际如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文