JSTL表达式语言访问对象属性 [英] JSTL Expression Language accessing object properties

查看:291
本文介绍了JSTL表达式语言访问对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天正在听一个教程,这个教程让我挠头了一个小时.考虑:

I was following a tutorial today that had me scratching my head for an hour. Consider:

public class MyClass {
    public int getTotal() {
        amount = 100;
        return amount;
    }
}

以及JSP的摘录:

<p>Total: ${objectOfTypeMyClass.total}</p> //object instantiated elsewhere

在代码中没有任何地方声明或使用过名为" total "的实例变量.在整个项目中(除了JSP中),唯一引用总计"一词的方法是 getTotal().

Nowhere in the code was an instance variable named "total" ever declared or used. The only reference to the word "total" in the whole project (other than in the JSP) was the method getTotal().

因此,在进行了一次绝望的最后一次尝试之后,表达式语言似乎将 $ {someObject.var} 评估为调用 getVar()方法> someObject 对象.

So after some desperate last-ditch experimentation, it appears that Expression Language evaluates ${someObject.var} as "call the getVar() method of the someObject object.

我在这个冗长的教程中工作了一个多星期,以为 $ {someObject.var} 是说直接从 var " > someObject .

I worked with this long tutorial for over a week thinking that ${someObject.var} was saying "directly fetch the saved instance variable "var" from someObject.

我一直都错吗?我的观察是否正确,为了使用EL引用任何实例变量,您必须提供一个名为 getVarname()的相应getter方法,其中" Varname "是实例变量的名称吗?

Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?

此外,EL在这方面似乎不区分大小写.在上面的示例中, $ {objectOfTypeMyClass.total} 中的"total"全部为小写字母,其中方法 getTotal()具有大写的"T".

Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".

在使用它时,为什么我们不需要实例化变量"total"呢?我猜EL实际上不是在引用实例变量...只是一个getter方法?

And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?

有什么作用?

推荐答案

我一直都错吗?我的观察是否正确,为了使用EL引用任何实例变量,您必须提供一个名为getVarname()的对应getter方法,其中"Varname"是实例变量的名称?

Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?

是正确的. EL遵守 JavaBeans规范,如 EL规范.

That's correct. EL adheres the JavaBeans specification as described in the EL specification.

此外,EL在这方面似乎不区分大小写.在上面的示例中,$ {objectOfTypeMyClass.total}中的"total"全部为小写字母,其中方法getTotal()的首字母为"T".

Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".

不,它当然不区分大小写.它是特定的行为. ${bean.Total}不会起作用.

No, it's certainly not case insensitive. It's specified behaviour. ${bean.Total} would not have worked.

在使用它时,为什么我们不需要实例化变量"total"呢?我猜EL实际上不是在引用实例变量...只是一个getter方法?

And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?

这是因为它应该遵守Javabean规范.

It's because it's supposed to adhere the Javabean specification.

所有内容全部阅读,两个规格都将清楚:)

All with all, read the both specifications and everything will be clear :)

这篇关于JSTL表达式语言访问对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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