Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method calls in EL

Tags:

java

jsp

jstl

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.

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

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.

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

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

like image 402
Maurice Perry Avatar asked Mar 28 '09 11:03

Maurice Perry


2 Answers

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

It's part of the bondage and discipline mindset of a certain subset of Java programmers that knows The One True Way that everyone should author applications.

You can't be trusted have a full-powered language at your disposal when writing templates, because you might abuse it to mix together business logic and presentation like those awful, uncouth PHP coders do. Eurgh, imagine!

The programmer is the enemy, and must be prevented from doing Evil. It might turn out that the programmer is actually just you trying to debug something, or put a quick hack in to get the app running temporarily. Or it might turn out that there is such a thing as presentation logic, and making you move that stuff out into a bunch of tag and bean classes in your application is just as unpleasant as the other way around.

But it doesn't matter! It's well worth inconveniencing you to serve the purpose of defending Java's chastity.

[Sorry for the snark, but it was already quite a ranty question eh.]

PS. There's always Groovy I suppose.

like image 97
bobince Avatar answered Sep 23 '22 01:09

bobince


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.

like image 29
McDowell Avatar answered Sep 22 '22 01:09

McDowell