Difference between LENGTH[...] and fn:length(...) in JSP EL

15,568

Since there is no such function like LENGTH[...] in standard JSP/JSTL/EL, it's impossible to tell about the differences. The fn:length() is the only way to obtain the length of a String, an Object[] or Collection.

${fn:length(someCollection)}

Update as per your (fixed) example:

<c:when test="${object.field ne null || LENGTH[object.field] > 0}"> 

I've never seen this before. It look like that your webapp/servletcontainer is using a custom EL resolver. If this is true, you should see it been declared in webapp's web.xml file.

Regardless, you'd rather like to use the EL empty keyword here. It not only checks for null, but also for the length of the String, Object[] or Collection.

<c:when test="${not empty object.field}"> 

No need for fn:length() here.


The brace notation [] is in turn by the way often used to access properties by dynamic keys. E.g.

${bean[propertyname]}

If propertyname resolves to "foo", then the above does effectively the same as ${bean.foo}. It's also often used on Map objects in the scope.

Share:
15,568
Sammy
Author by

Sammy

Updated on June 05, 2022

Comments

  • Sammy
    Sammy almost 2 years

    What is the difference between the LENGTH[...] and the JSTL function fn:length(...)?

    I tried to search to difference but I did not see any example that uses the first one.

    Here is an example:

    <c:when test="${object.field ne null || LENGTH[object.field] > 0}"> 
        <td valign="top">
            .....print something
        </td>
    </c:when>