JSTL 表达式语言访问对象属性

发布于 2024-09-24 16:27:14 字数 1070 浏览 5 评论 0原文

我今天正在学习一个教程,这个教程让我摸不着头脑一个小时。考虑一下:

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

以及 JSP 的摘录:

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

代码中没有任何地方声明或使用过名为“total”的实例变量。整个项目(JSP 中除外)中唯一引用“total”一词的是方法 getTotal()

因此,经过一些绝望的最后一搏实验后,表达式语言将 ${someObject.var} 计算为“调用 someObject< 的 getVar() 方法” ”

我花了一个多星期的时间研究这个冗长的教程,认为 ${someObject.var} 是在说“直接从其中获取保存的实例变量“var 某个对象

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

此外,EL 在这方面似乎不区分大小写。在上面的示例中,${objectOfTypeMyClass.total} 中的“total”全部为小写,而方法 getTotal() 的“T”大写。

当我们这样做时,为什么我们不需要实例化变量“total”呢?我猜 EL 实际上并没有引用实例变量......只是一个 getter 方法?

什么给?

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;
    }
}

and an excerpt from a JSP:

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

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().

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

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.

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?

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".

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?

What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-10-01 16:27:14

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

这是正确的。 EL 遵循 JavaBeans 规范,如 < a href="http://download.oracle.com/otn-pub/jcp/expression_language-2.2-mrel-eval-oth-JSpec/expression_language-2_2-mrel-spec.pdf" rel="nofollow noreferrer">EL规范。

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

不,它肯定不区分大小写。这是指定的行为。 ${bean.Total} 不会起作用。

既然我们这样做了,为什么我们不需要实例化变量“total”呢?我猜 EL 实际上并没有引用实例变量...只是一个 getter 方法?

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

总而言之,阅读这两个规范,一切都会清楚:)

另请参阅:

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?

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

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".

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

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?

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

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

See also:

心欲静而疯不止 2024-10-01 16:27:14

objectOfTypeMyClass.total 中的 . 是 JSTL EL 点运算符。它可以做一些不同的事情。其中:

  1. map.key访问key下存储的map中的值。或
  2. object.property 使用“JavaBeans”约定从 object 访问 property

The . in objectOfTypeMyClass.total is the JSTL EL Dot Operator. It can do a few different things. Including:

  1. map.key accessed a value from map stored under key. or
  2. object.property accesses property from object using "JavaBeans" conventions.
雅心素梦 2024-10-01 16:27:14

这应该有效:

public class MyClass {

    private int total = 100;

    public int getTotal() {
        return total;
    }

    ...
}

This should work:

public class MyClass {

    private int total = 100;

    public int getTotal() {
        return total;
    }

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文