t0mcat
t0mcat

Reputation: 5679

JSTL EL conditional error

I am using the following conditional check using JSTL but its throwing the error "javax.servlet.jsp.el.ELException: No function is mapped to the name "fn:length"
<c:choose>
<c:when test='${fn:length(studentData.rollNumber) == "0"}'> Found Nothing </c:when> <c:otherwise> Found something </c:otherwise> </c:choose>

What am I doing wrong here? I just need to compare the length of roll number.

Upvotes: 0

Views: 8663

Answers (2)

BalusC
BalusC

Reputation: 1108702

As per the documentation, the fn:length() only works on String (which would return the value of String#length() method) and on Collection (which would return the value of Collection#size() method).

You however seem to be passing in a number. An integer or something. The fn:length() doesn't work on numbers and would always give false, irrespective of the number's value.

If you want to check if something is null, then just do so:

<c:choose>
    <c:when test="${studentData.rollNumber == null}">Found Nothing</c:when> 
    <c:otherwise>Found something</c:otherwise>
</c:choose>

Or if you want to check if the number's value is 0, then just do

<c:choose>
    <c:when test="${studentData.rollNumber == 0}">Found Nothing</c:when> 
    <c:otherwise>Found something</c:otherwise>
</c:choose>

Note that the empty check works equally well and this is regardless of whether it's a number, string or a collection. Anything which is null or has a fn:length() of 0 would evaluate true.

<c:choose>
    <c:when test="${empty studentData.rollNumber}">Found Nothing</c:when> 
    <c:otherwise>Found something</c:otherwise>
</c:choose>

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240890

Add

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

And make condition like the following

<c:when test="${fn:length(studentData.rollNumber) == 0}">

Upvotes: 1

Related Questions