JSTL经典代码片段分享

 

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} 




 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值