Does EL automatically convert/cast the type? How does ${a.name} actually work?

南笙酒味 提交于 2019-11-30 22:45:20
BalusC

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

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.

See also:

It's because name is a property of the object a, and probably the object is also a JavaBean (not to be confused with Enterprise JavaBean).

See here for Expression Language Documentation and here for a short tutorial.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!