gpt4 book ai didi

java - JSTL EL 条件错误

转载 作者:行者123 更新时间:2023-12-02 00:41:27 25 4
gpt4 key购买 nike

我正在使用 JSTL 使用以下条件检查,但它抛出错误“javax.servlet.jsp.el.E​​LException:没有函数映射到名称“fn:length”

<c:choose><br/>
<c:when test='${fn:length(studentData.rollNumber) == "0"}'>
Found Nothing
</c:when>
<c:otherwise>
Found something
</c:otherwise>
</c:choose>

我在这里做错了什么?我只需要比较卷数的长度。

最佳答案

根据 documentationfn:length() 仅适用于 String(这将返回 String#length() 方法的值)和 Collection(这将返回返回 Collection#size() 方法的值)。

但是,您似乎正在传递一个数字。一个整数什么的。 fn:length() 不适用于数字,并且无论数字的值如何,总是给出 false

如果您想检查某些内容是否为null,那么就这样做:

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

或者,如果您想检查数字的值是否为0,那么只需执行

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

请注意,empty 检查同样有效,无论它是数字、字符串还是集合。任何 nullfn:length()0 的值都会评估为 true

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

关于java - JSTL EL 条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6270543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com