EL中的方法调用 [英] Method calls in EL

查看:64
本文介绍了EL中的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写Java webapps时,我通常使用JSTL标记。我认为这些标签很棒,除了一件令我生气的事情:虽然表达式语言允许你访问bean属性,但它不允许你调用它的方法。

When I write Java webapps, I usually use JSTL tags. I think that these tags are great, except for one thing that pisses me off: while the expression language allow you to access bean properties, it does not allow you to call its methods.

在1.0版中,甚至无法获得字符串的长度或集合中的元素数。从版本1.1开始,添加了fn:length函数,因此您可以执行以下操作:

In release 1.0, it was not even possible to obtain the length of a string or the number of elements in a collection. As of release 1.1, the fn:length function has been added, so you can do things such as this:

...
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:if test="${fn:length(str) > 10}">
    ...
</c:if>
...

哪个更啰嗦,更丑陋(恕我直言)比:

Which is more verbose and more ugly (IMHO that is) than:

...
<c:if test="${str.length() > 10}">
    ...
</c:if>
...

它接缝JSTL 2.0将允许您定义新功能,但是您将需要专门为此目的编写一个类,您将在其中定义(静态)方法,并且还需要编写将包含在将使用这些函数的每个jsp中的TLD文件。

It seams that JSTL 2.0 will allow you to define new functions, but you will need to write a class specifically for that purpose, in which you will define your (static) methods, and you will also need to write a TLD file that will be included in every jsp that will use these functions.

无论您是定义自定义函数还是使用其他解决方法,都需要编写大量其他代码。

Whether you define custom function or you use another workaround, you have a lot of additional code to write.

我已阅读在某个地方,JCP自愿禁止从表达语言中调用方法。

I have read somewhere that the JCP had voluntarily disallow the calling of methods from the expression language.

你们中的任何人都可以帮助我理解为什么JCP会对我们这样做吗?

Can anyone of you help me understand why the hell is the JCP doing this to us?

推荐答案

您想要的功能在 JSR 245 此处更多)。如果您现在需要,请从 UEL项目或替代实施(例如 JUEL )。如果您需要等待它成为标准的一部分,它将包含在JEE6中。在那之前......好吧,你已经知道了你的选择。

The feature you want is defined in JSR 245 (more here). If you want it now, go download it from the UEL project or an alternative implementation (e.g. JUEL). If you need to wait for it to be part of the standard, it will be included in JEE6. Until then... well, you already know your options.

这篇关于EL中的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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