jstl标签使用

本文介绍了JSTL中的<c:if>和<c:forEach>标签的使用方法,包括条件判断、集合与字符串判空及循环取值等应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在项目中遇到一些新的知识,JSTL表达式,代替了以往的在JSP中的<%%>的写法, 看上去整洁了许多。随手记录一下。

 一、<C:If>标签:条件判断语句

[html]  view plain  copy
  1. <c:if test="${objList.nodetype == 1}">上级节点</c:if>  
test为if语句的判断条件。执行与java中的一致。

简单总结几种判空的处理:

1、集合判空。利用关键字  empty

[html]  view plain  copy
  1. <c:if test="${ empty list}">   
  2.   
  3. //要执行的语句...  
  4.   
  5. </c:if>  

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

[html]  view plain  copy
  1. <c:if test="${mdxDimensionInfoList=='[]'}">  
  2.   
  3. //要执行的代码...  
  4.   
  5. </c:if>  

3、字符串判空

[html]  view plain  copy
  1. <c:if test="${query01 == null}">  
  2.   
  3.   //执行代码...  
  4.   
  5. </c:if>  
  6.   
  7. 或者  
  8.   
  9. <c:if test="${query01 == ''}">  
  10.   
  11.   //执行代码...  
  12.   
  13. </c:if>  

4、判断两个字符创是否相等



二、<C:ForEach>标签,循环取值

       <C:ForEach> 一般使用的两个重要属性,

        items------要循环遍历的集合

        var----------迭代器名称,通俗点说就是用var来 取值,(见下)

        C:out-------通过C:Out将值显示在页面

[html]  view plain  copy
  1. <c:forEach items="${mdxMeasureInfoList}" var="obj">  
  2.     <tr>  
  3.            <td width="20%"><c:out value="${obj.measureName}"></c:out></td>  
  4.            <td width="20%"><c:out value="${obj.myFildName}"></c:out></td>  
  5.            <td width="20%"><c:out value="${obj.unit}"></c:out></td>  
  6.            <td width="20%"><c:out value="${obj.calFormula}"></c:out></td>  
  7.     </tr>  
  8. </c:forEach>  

例子: C:if + C:forEach

 

[html]  view plain  copy
  1. //<c:forEach> 与 <c:if>分开使用  
  2.   
  3. <table>  
  4.     <tbody>  
  5.         <c:forEach items="${mdxMeasureInfoList}" var="obj">  
  6.             <tr>  
  7.                 <td width="20%"><c:out value="${obj.measureName}"></c:out></td>  
  8.                 <td width="20%"><c:out value="${obj.myFildName}"></c:out></td>  
  9.                 <td width="20%"><c:out value="${obj.unit}"></c:out></td>  
  10.                 <td width="20%"><c:out value="${obj.calFormula}"></c:out></td>  
  11.             </tr>  
  12.         </c:forEach>  
  13.   
  14.         <c:if test="${mdxMeasureInfoList=='[]'}">  
  15.             <tr>  
  16.                 <td colspan="11"><font color="red">很抱歉 没有可以展示的数据!</font></td>  
  17.             </tr>  
  18.         </c:if>  
  19.     </tbody>  
  20. </table>  
  21.   
  22.   
  23.   
  24.   
  25. //例子2:<c:forEach> 与 <c:if>嵌套使用  
  26.   
  27. <table>  
  28.     <tbody>  
  29.   
  30.                 //page.content,是因为在controller中将传过来的list封装在了pagebean实体中,  
  31.                 //所以取值时为page.content。普通集合取值时,不用加 .content  
  32.   
  33.         <c:forEach items="${page.content}" var="obj">  
  34.             <tr>  
  35.                 <td width="30px"><input name="id" type="checkbox" value="${obj.id}" /></td>  
  36.                 <td width="15%"><c:out value="${obj.measureName}"></c:out></td>  
  37.                 <td width="15%"><c:out value="${obj.myFildName}"></c:out></td>  
  38.                 <td width="15%"><c:out value="${obj.unit}"></c:out></td>  
  39.                 <td width="15%">  
  40.                 <c:if test="${obj.measureAggregator=='sum'}">总和</c:if>  
  41.                 <c:if test="${obj.measureAggregator=='count'}">计数</c:if>  
  42.                 <c:if test="${obj.measureAggregator=='min'}">最小值</c:if>  
  43.                 <c:if test="${obj.measureAggregator=='max'}">最大值</c:if>  
  44.                 <c:if test="${obj.measureAggregator=='avg'}">平均值</c:if>  
  45.                 </td>  
  46.                 <td width="15%">  
  47.                 <c:if test="${obj.ifDisplay=='0'}">显示</c:if>  
  48.                 <c:if test="${obj.ifDisplay!='0'}">隐藏</c:if>  
  49.                 </td>  
  50.             </tr>  
  51.         </c:forEach>  
  52.     </tbody>  
  53. </table>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值