Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

java - Method calls in EL

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...