Reputation: 2614
I'm trying to ensure that list of objects doesn't null and has at least one item in it. Please tell me what am I doing wrong with the following snippet.
<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>
I found out it stop right at the if statement by debug, and hmlt tags inside hasn't been rendered even if there's item in educations list
Upvotes: 0
Views: 741
Reputation: 8553
Have you included properly for JSTL functions ?
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
And also add prefix requestScope before accessing any variables.
<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>
It would be better if you do only the empty
check
Your intent is to iterate over the list using then it may be good to know that it already won't run when the provided items is empty. If the is directly surrounded by this check, then this check is entirely superfluous.
Upvotes: 1