JSP JSTL平常使用集錦

根據本身使用不斷積累更新html

1、JSTL 使用 c:forEach 累加變量值  java

使用舉例以下:git

            <tbody>
                <c:set value="0" var="amountTotal"/>
                <c:forEach items="${medicine.medicineInformationVos}" var="medicine">
                <tr>
                    <td>${medicine.code }</td>
                    <td>${medicine.name }</td>
                    <td>${medicine.amount }</td>
                </tr>
                <c:set value="${amountTotal+medicine.quantity}" var="amountTotal"/>
                </c:forEach>
                <tr>
                    <td></td>
                    <td></td>
                    <td>${amountTotal}</td>
                </tr>
            </tbody>

最後一行tr的最後一個td是統計上面循環出來的medicine.amount的總和。jsp

2、jstl對數字的一些格式化操做spa

可參考   <fmt:formatNumber>標籤 的一些用法 :code

//好比對傳進來的浮點數字${total},進行只保留兩位小數操做
<fmt:formatNumber type="number" value="${total}" maxFractionDigits="2"/>

 3、forEach循環每行兩和三列的顯示方法orm

1.每行兩列htm

<c:forEach  items = " ${list} " varStatus = " status " var="medicine"> 
 <c: if test = " ${status.count%2==1} "> 
 <tr> 
 </c: if> 
 <td>${medicine.name }</td> 
 <c: if test = " ${status.count%2==0} "> 
 </tr> 
 </c: if > 
 <c:set var = " v_count " value = " ${status.count} "/> 
 </c:forEach> 
 <c:if test = " ${v_count%2==1} "> 
 <td></td> 
 </tr> 
 </c: if>

2.每行三列blog

<c:forEach  items="${list}" var="medicine" varStatus="status"> 
<c:if test="${status.count%3==1}">
<tr>
</c:if> 
<td>${medicine.name }</td>
<c:if test="${status.count%3==0}">
</tr>
</c:if>
<c:set var ="v_count" value="${status.count}"/>
</c:forEach>
<c:if test="${v_count%3==1}">
<td></td>
<td></td>
</tr>
</c:if>
<c:if test="${v_count%3==2}">
<td></td>
</tr>
</c:if>  

4、EL表達式 獲取list長度/不用循環ci

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

${fn:length(list名字)}   //list的長度
<c:if test="${fn:length(list名字)>1}">
  中間該幹嗎幹嗎
</c:if>

5、不用循環,EL在List中直接獲取第一項的內容

${list[0].屬性}
相關文章
相關標籤/搜索