jsp判断List长度

大家好:

今天遇到的一个问题就是后台往前台传List ,前台判断List的长度!

查别人的都说要加别的标签

<span style="font-size:14px;color:#cc0000;">jsp开发中经常要判断list或者字符串的长度来做些事情,因为jstl标签中的if不支持else,所以选择while来做。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>--------这里就需要有standard.jar这个包
引入两个标签库之后就能实现长度判断了,例如
Jsp代码  收藏代码

    <c:choose>  
        <c:when test="${fn:length(contact.tagschosen) == 0}">  
        <span class="grouped">未分组</span>  
        </c:when>  
        <c:otherwise>  
        <c:forEach items="${contact.tagschosen}" var="tags">  
            <span class="grouped">${tags.tagname}</span>  
        </c:forEach>  
        </c:otherwise>  
    </c:choose>  </span>
而我今天要说的是只需要 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 这个标签就好了

只需要在 body中加入个隐式标签就可以轻轻松松判断了

<input type="hidden" name="tcMedicineList" value="${tcMedicineList}" />

  <c:if test="${tcMedicineList.size()>2}"> 

这样就OK了!!!

<span style="font-size:14px;"><body>
		<input type="hidden" name="tcMedicineList" value="${tcMedicineList}" />
		<input type="hidden" name="weMedicineList" value="${weMedicineList}" />
		<input type="hidden" name="otMedicineList" value="${otMedicineList}" />
		<input type="hidden" name="trMedicineList" value="${trMedicineList}" />
		<input type="hidden" name="ptcMedicineList" value="${ptcMedicineList}" />
<!--recipe start-->
<div class="container">
    <dl class="recipe_pay_detail recipe_detail">
    
        <dt class="medicine_head"><i class="ico ico_recipe recipe_bg6"></i>中药药品:</dt>
        
         <dd id="medicine_info_chn">
          
       </span><span style="font-size:18px;color:#ff6600;"> <span style="background-color: rgb(102, 0, 204);"><c:forEach items="${tcMedicineList}" var="tcMedicineList" begin="0" end="1"></span></span><span style="color: rgb(255, 102, 0);font-size:18px; font-family: Arial, Helvetica, sans-serif;"> </span><span style="color: rgb(255, 102, 0);font-size:18px; font-family: Arial, Helvetica, sans-serif;"><!-- 循环List只显示前两条 --></span>
<span style="font-size:14px;">              <p><span>${tcMedicineList.name}</span>${tcMedicineList.standard} </p>
        </c:forEach> 
        
  </span><strong style="font-size:14px;">   </strong><span style="font-size:18px;">  </span><span style="color:#ff9900;"><span style="font-size:18px;"><span style="background-color: rgb(51, 102, 255);">  <c:if test="${tcMedicineList.size()!=0&&2>tcMedicineList.size()}"></span> </span><span style="font-size:14px;">  </span></span><span style="color: rgb(255, 102, 0); font-size: 18px; font-family: Arial, Helvetica, sans-serif;"><!-- 如果List条数不等于0 并且条数小于2条时 --></span><span style="font-size:14px;"><span style="color:#ff9900;">
</span>
          <c:forEach items="${ptcMedicineList}" var="ptcMedicineList"<span style="background-color: rgb(51, 102, 255);"><span style="color:#ff0000;"> begin="0" end="0"</span></span>>
          <p>
          	剂量:${ptcMedicineList.num} ${ptcMedicineList.unit}<br/>
		   <%--       剂量:${ptcMedicineList.dosage}<br> --%>
		        用法:${ptcMedicineList.usemethod} ${ptcMedicineList.dosage}
          </p>
        </c:forEach>
           </c:if>
           
         <c:if test="${tcMedicineList.size()>2}"> </span><span style="color: rgb(255, 102, 0); font-family: Arial, Helvetica, sans-serif; font-size: 18px;"><!-- 如果List条数大于2条时 --></span><span style="font-size:14px;">

           <c:forEach items="${ptcMedicineList}" var="ptcMedicineList" <span style="background-color: rgb(51, 102, 255);"><span style="color:#ff6600;">begin="0" end="0"></span></span><span style="color:#ffff66;">></span> </span><span style="color: rgb(255, 102, 0); font-family: Arial, Helvetica, sans-serif;font-size:18px;"><!-- 循环List只显示前一条 --></span><span style="font-size:14px;">

          <p>
		      剂量:${ptcMedicineList.num} ${ptcMedicineList.unit}<br/>
		        用法:${ptcMedicineList.usemethod} ${ptcMedicineList.dosage}
            <a href="#" class="btn btn_o_c btn_open" id="btn_open_chn">展开</a>
          </p>
        </c:forEach>
        </c:if>
      
         </dd>
         
        <dd id="medicine_detail_chn">
        <c:forEach items="${tcMedicineList}" var="tcMedicineList">
            <p> <span>${tcMedicineList.name}</span>${tcMedicineList.standard}</p> 
          </c:forEach>
       <c:forEach items="${ptcMedicineList}" var="ptcMedicineList" begin="0" end="0"></span><span style="color: rgb(255, 102, 0); font-family: Arial, Helvetica, sans-serif;font-size:18px;"><!-- 循环List只显示前一条 --></span><span style="font-size:14px;">

          <p>
		      剂量:${ptcMedicineList.num} ${ptcMedicineList.unit}<br/>
		        用法:${ptcMedicineList.usemethod} ${ptcMedicineList.dosage}
             <a href="#" class="btn btn_o_c btn_close" id="btn_close_chn">收起</a> 
           </p>
          </c:forEach>
        </dd>
       
        <dt  class="medicine_head"><i class="ico ico_recipe recipe_bg4"></i>西药药品:</dt>
        
         <dd id="medicine_info">
        <c:forEach items="${weMedicineList}" var="weMedicineList" begin="0" end="1">
	        <p><span>${weMedicineList.name}</span>${weMedicineList.standard}</p>
       </c:forEach>
       <c:if test="${weMedicineList.size()>2}">
            <p>...<a href="#" class="btn btn_o_c btn_open" id="btn_open">展开</a></p>
       </c:if>
        </dd>
        
        
        <dd id="medicine_detail">
          <c:forEach items="${weMedicineList}" var="weMedicineList">
            <p><span>${weMedicineList.name}</span>${weMedicineList.standard}</p>  
          </c:forEach>
           <p> <a href="#" class="btn btn_o_c btn_close" id="btn_close">收起</a></p>
        </dd>
        
     <%-- 
     <dt> <i class="ico ico_recipe recipe_bg4"></i>检查项目:</dt>
         <dd id="medicine_info_check">
        <c:forEach items="${entity}" var="entity" begin="0" end="1">
	        <p><span>${entity.name}</span></p>
       </c:forEach>
            <p><a href="#" class="btn btn_o_c btn_open" id="btn_open">展开</a></p>
        </dd>
        <dd id="medicine_detail_check">
          <c:forEach items="${entity}" var="entity">
            <p><span>${entity.name}</span></p>  
          </c:forEach>
           <p> <a href="#" class="btn btn_o_c btn_close" id="btn_close">收起</a></p>
        </dd>
      --%>
      
      <dt><i class="ico ico_recipe recipe_bg5"></i>其他:</dt>
      <dd>
      <c:forEach items="${otMedicineList}" var="otMedicineList">
        	 <p><span>${otMedicineList.name}</span>${otMedicineList.standard}</p>
      </c:forEach>
      </dd>
      <dt><i class="ico ico_recipe recipe_bg2"></i>治疗项目:</dt>
      <c:forEach items="${trMedicineList}" var="trMedicineList">
      		<dd><p><span>${trMedicineList.name}</span>${trMedicineList.standard}</p></dd>
      </c:forEach>
         <dd class="recipe_sum sum_all">合计:${fee} 元</dd>
    </dl>
</div></span>




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。