Unable to find a value for “length” in object of class “java.lang.String” using operator “.”

Why does this JSTL expression not seem to be able to use the length method of the String class:

    <c:when test="${displayName != null && displayName.length > 0 }">
        <p><c:out value="${displayName}"/></p>

It produces this exception:

java.lang.RuntimeException: javax.servlet.ServletException: 
javax.servlet.jsp.el.ELException: Unable to find a value for "length" in object 
of class "java.lang.String" using operator "."

I have included the following tag libraries at the top of the JSP:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>


You can access only getters (getSomething) like this. To find string size, try fn:length(displayName). Naturally, fn namespace should be imported:

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

Also, you can replace this all with empty check: ${not empty displayName}

Leave a Reply

Your email address will not be published. Required fields are marked *