JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入门级~

一、<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> 
复制代码

 


 

posted @   QiaoZhi  阅读(12399)  评论(3编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示