JSTL 和 入門級~

最近在項目中遇到一些新的知識,JSTL表達式,代替了以往的在JSP中的<%%>的寫法, 看上去整潔了許多。隨手記錄一下。

 一、<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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章