tags:

views:

405

answers:

4
+1  Q: 

Method calls in EL

Hi there,

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?

A: 

I expect the goal was to enforce strict model-view separation, as in StringTemplate.

The idea is pretty simple: If you don't have access to methods, then you are forced to compute all of your data in the controller and then just format it in the view. Of course they muddied the message by allowing you to have access to functions and they were not helped by the fact that it is not as easy to build up lists of maps of tuples (read: structured data without classes) in Java as it is in Python, Ruby, etc.

Anyway, here's an article on writing a tag that allows you to call arbitrary methods, might find it helpful.

Aaron Maenpaa
Well I don't get it: it's still possible to do it, it's just more painful. Besides, it often forces the model to be aware of what is done in the view while IMHO the view should have visibility on the model and not the other way around.
Maurice Perry
Most of the point of the View in MVC as opposed to direct manipulation is that the view has logic to transform the model, much like a view in SQL can join and filter multiple tables. Restricting it to a templating engine seems daft.
Pete Kirkham
+2  A: 

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.

McDowell
That's a relief!!! thanks!
Maurice Perry
+3  A: 

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.

bobince
LOL. I appreciate your humor
Maurice Perry
+1  A: 

Have a look at Seam! With Seam, EL became the way it should be. Calling methods even with parameters works like a breeze.