JSTL经典代码片段分享

来源:互联网 发布:广州网络教育本科 编辑:程序博客网 时间:2024/06/05 20:28

 

JSTL官方在线帮助文档:http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html 

 

1、循环遍历List:(http://stackoverflow.com/questions/11954812/why-is-my-jstl-cif-test-should-be-true-returning-even-the-false-items)

 <c:forEach var="value" items="${valueList}" varStatus="status">         <c:if test="${value.displayable}">  <c:if test="${status.index%2==0 }">  <li>  <a href=""> title </a> <a href=""> link </a>  </li>  </c:if> <c:if test="${status.index%2!=0 }"> <li class="Odd">  <a href=""> title </a>  <a href=""> link </a> </li> </c:if> </c:if>        </c:forEach> 


2、循环遍历MAP:(http://stackoverflow.com/questions/11884774/how-to-use-the-index-variable-of-a-jstl-foreach-loop-to-access-a-map-entry)

 <c:forEach items="${aMapWithData}" var="item" varStatus="status">      <td>          <c:out value="${status.count}."/>           <input type="text" name="${item.key}" value="${item.value}" />      </td>   </c:forEach> 

 


3、c:when标签:(http://stackoverflow.com/questions/11898583/how-to-use-cwhen-condition-inside-value-attribute-of-input-tag)

 <c:choose> <c:when test="${texttest == 'Y'}">  Add </c:when> <c:otherwise>  Edit  </c:otherwise>  </c:choose>
 <input id="textid" type="button" value="${texttest == 'Y' ? 'Add' : 'Edit'}">

 

4、lt 的用法:(http://stackoverflow.com/questions/11878188/what-does-this-lt-mean-in-jstl)

       lt 即是less than 的缩写,相当于< ,它是EL操作符

 <c:if test="${currentPage lt noOfPages}">             <td><a href="employee.do?page=${currentPage + 1}">Next</a></td>          </c:if> 



5、<c:set>和<c:forEach>组合(http://stackoverflow.com/questions/11735059/how-to-update-value-in-cset-tag-using-el-inside-a-cforeach-tag)

    

In Java, it would look like this:

// before the loop: int totalHours = 0; for (Attendance attendance : list) {     totalHours = totalHours + attendance.getHours(); }

等效于 

<c:set var="totalHours" value="${0}"/> <c:forEach var="attendance" items="${list }" varStatus="rowCounter1">     ...     <c:set var="totalHours" value="${totalHours + attendance.hours}"/> </c:forEach> 


 6、如果值中含有html code 或者是url等值的时候,处理办法如下:(http://stackoverflow.com/questions/11701652/url-jstl-database)

<c:out value="${cursus.beschrijving}" escapeXml="false" />

或者这样写

${cursus.beschrijving} 




 

 


 

原创粉丝点击