1. 程式人生 > >JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入門級~

JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入門級~

ava 條件 title spa b2c colspan geb 屬性 oar

一、<C:If>標簽:條件判斷語句

<c:if test="${objList.nodetype == 1}">上級節點</c:if>  

test為if語句的判斷條件。執行與java中的一致。

簡單總結幾種判空的處理:

1、集合判空。利用關鍵字 empty

<c:if test="${ empty list}">   
  
//要執行的語句...  
  
</c:if>  


2、集合判空的另一種方式


<
c:if test="${mdxDimensionInfoList==‘[]‘}"> //要執行的代碼... </c:if>

3、字符串判空

<c:if test="${query01 == null}">  
  
  //執行代碼...  
  
</c:if>  
  
或者  
  
<c:if test="${query01 == ‘‘}">  
  
  //執行代碼...  
  
</c:if>  



4、判斷兩個字串是否相等

技術分享


二、<C:ForEach>標簽,循環取值

<C:ForEach> 一般使用的兩個重要屬性,

items------要循環遍歷的集合

var----------叠代器名稱,通俗點說就是用var來 取值,(見下)

C:out-------通過C:Out將值顯示在頁面

<c:forEach items="${mdxMeasureInfoList}" var="obj">  
    <
tr> <td width="20%"><c:out value="${obj.measureName}"></c:out></td> <td width="20%"><c:out value="${obj.myFildName}"></c:out></td> <td width="20%"><c:out value="${obj.unit}"></c:out></td> <td width="20%"><c:out value="${obj.calFormula}"></c:out></td> </tr> </c:forEach>

例子: C:if + C:forEach

//<c:forEach><c:if>分開使用  
  
<table>  
    <tbody>  
        <c:forEach items="${mdxMeasureInfoList}" var="obj">  
            <tr>  
                <td width="20%"><c:out value="${obj.measureName}"></c:out></td>  
                <td width="20%"><c:out value="${obj.myFildName}"></c:out></td>  
                <td width="20%"><c:out value="${obj.unit}"></c:out></td>  
                <td width="20%"><c:out value="${obj.calFormula}"></c:out></td>  
            </tr>  
        </c:forEach>  
  
        <c:if test="${mdxMeasureInfoList==‘[]‘}">  
            <tr>  
                <td colspan="11"><font color="red">很抱歉 沒有可以展示的數據!</font></td>  
            </tr>  
        </c:if>  
    </tbody>  
</table>  
  
  
  
  
//例子2:<c:forEach><c:if>嵌套使用  
  
<table>  
    <tbody>  
  
                //page.content,是因為在controller中將傳過來的list封裝在了pagebean實體中,  
                //所以取值時為page.content。普通集合取值時,不用加 .content  
  
        <c:forEach items="${page.content}" var="obj">  
            <tr>  
                <td width="30px"><input name="id" type="checkbox" value="${obj.id}" /></td>  
                <td width="15%"><c:out value="${obj.measureName}"></c:out></td>  
                <td width="15%"><c:out value="${obj.myFildName}"></c:out></td>  
                <td width="15%"><c:out value="${obj.unit}"></c:out></td>  
                <td width="15%">  
                <c:if test="${obj.measureAggregator==‘sum‘}">總和</c:if>  
                <c:if test="${obj.measureAggregator==‘count‘}">計數</c:if>  
                <c:if test="${obj.measureAggregator==‘min‘}">最小值</c:if>  
                <c:if test="${obj.measureAggregator==‘max‘}">最大值</c:if>  
                <c:if test="${obj.measureAggregator==‘avg‘}">平均值</c:if>  
                </td>  
                <td width="15%">  
                <c:if test="${obj.ifDisplay==‘0‘}">顯示</c:if>  
                <c:if test="${obj.ifDisplay!=‘0‘}">隱藏</c:if>  
                </td>  
            </tr>  
        </c:forEach>  
    </tbody>  
</table> 


JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入門級~