JSTL 表达式语言访问对象属性
我今天正在学习一个教程,这个教程让我摸不着头脑一个小时。考虑一下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是正确的。 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规范。
不,它肯定不区分大小写。这是指定的行为。
${bean.Total}
不会起作用。这是因为它应该遵守 Javabean 规范。
总而言之,阅读这两个规范,一切都会清楚:)
另请参阅:
That's correct. EL adheres the JavaBeans specification as described in the EL specification.
No, it's certainly not case insensitive. It's specified behaviour.
${bean.Total}
would not have worked.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:
objectOfTypeMyClass.total
中的.
是 JSTL EL 点运算符。它可以做一些不同的事情。其中:map.key
访问key
下存储的map
中的值。或object.property
使用“JavaBeans”约定从object
访问property
。The
.
inobjectOfTypeMyClass.total
is the JSTL EL Dot Operator. It can do a few different things. Including:map.key
accessed a value frommap
stored underkey
. orobject.property
accessesproperty
fromobject
using "JavaBeans" conventions.这应该有效:
This should work: