EL 会自动转换/转换类型吗?${a.name} 实际上是如何工作的? [英] Does EL automatically convert/cast the type? How does ${a.name} actually work?

查看:22
本文介绍了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 会自动转换/转换类型吗?${a.name} 实际上是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆