为什么这个JSTL表达式似乎不能使用String类的length方法:
<c:when test="${displayName != null && displayName.length > 0 }">
<p><c:out value="${displayName}"/></p>
</c:when>
它产生了这个异常:
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 "."
我在 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" %>
最佳答案
您只能像这样访问 getter (getSomething
)。要查找字符串大小,请尝试 fn:length(displayName)
。当然,应该导入 fn
命名空间:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
此外,您可以将其全部替换为空
检查:${not empty displayName}
关于java - 使用运算符 "length"无法在类 "java.lang.String"的对象中找到 "."的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3488942/