0

我试图确保对象列表不为空并且其中至少包含一项。请告诉我下面的代码片段我做错了什么。

<c:if test=" ${ (not empty educations) && (fn:length(educations) ge 1) }">
            <c:forEach items="${educations}" var="edu">
                <div class="educations">                    
                    <label>Position</label><input type="text" name="${ edu.index }"  /><br/>
                    <label>School</label><input type="text" name="${ edu.school }" /><br/>
                    <label>Degree</label><input type="text" name="${ edu.degree }"   /><br/>
                    <label>GPA</label><input type="text" name="${ edu.scored }"   /><br/>
                    <label>Start Date</label><input type="text" name="${ edu.startDate }"   /><br/>
                    <label>End Date</label><input type="text" name="${ edu.endDate }"  /><br/>
                </div>
            </c:forEach>        
        </c:if>

我发现它在调试的 if 语句处停止,并且即使教育列表中有项目,内部的 HMLT 标记也没有呈现

4

1 回答 1

1

您是否正确包含了 JSTL 功能?

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

并且还在访问任何变量之前添加前缀 requestScope。

<c:if test=" ${not empty requestScope.educations}">
        <c:forEach items="${requestScope.educations}" var="edu">
            <div class="educations">                    
                <label>Position</label><input type="text" name="${ edu.index }"  /><br/>
                <label>School</label><input type="text" name="${ edu.school }" /><br/>
                <label>Degree</label><input type="text" name="${ edu.degree }"   /><br/>
                <label>GPA</label><input type="text" name="${ edu.scored }"   /><br/>
                <label>Start Date</label><input type="text" name="${ edu.startDate }"   /><br/>
                <label>End Date</label><input type="text" name="${ edu.endDate }"  /><br/>
            </div>
        </c:forEach>        
</c:if>

如果你只做empty检查会更好

您的意图是使用 then 遍历列表,最好知道当提供的项目为空时它已经不会运行。如果 直接被这个检查包围,那么这个检查完全是多余的。

于 2013-04-01T07:19:15.407 回答