2013-04-01 70 views
0

我试图确保对象列表不为空,并且至少有一个项目在其中。请告诉我,我在做什么错了下面的代码片段。if/else in jstl

<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标签内还没有被渲染,即使有在的教育列表项

+0

你检查了这个http://stackoverflow.com/questions/2811626/evaluate-empty-or-null-jstl-c-tags –

+0

是的。和已经http://stackoverflow.com/questions/9103082/jstl-condition-with-multiple-operators,但仍然无法弄清楚我的代码段出了什么问题。它看起来很好,但没有正常工作 –

+0

它不能正常工作?我没有看到任何明显的语法错误。 – Makoto

回答

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检查

你的目的是要遍历使用那么它可能是很好的知道,它已经不能运行时所提供的项目是列表空。如果直接被这个检查包围,那么这个检查完全是多余的。

+0

是的。我包括它。 FN:长度函数工作正常。唯一的问题是if语句。 –

+0

您是否尝试添加requestScope ..检查编辑。添加$ {}来获得'educations'的长度 – Meherzad

+0

我按照你的建议尝试了,并且得到了EL语法错误警告 –