在 jsp页面中遍历list中的数据

         往往我们都会将查询到的数据显示到界面中,那么该如何在界面显示,请看下面的详解:
    0)前提得在jsp页面中获取后台传过来的数据(在此为List集合):             
       <%
        List itemCategoryList=(List)request.getAttribute("itemCategoryList");
        %>
    往往在前台显示的时候回使用三种方式,一个是使用for循环,另一个是使用循环迭代器iterator,最后还有一种是使用JSTL。下面分别来看如何进行操作:
    1)使用for循环方式:    
        <select name="category" class="select1" id="category">
           <%
              for (int i=0;i<itemCategoryList.size();i++){
                 ItemCategory ic=(ItemCategory)itemCategoryList.get(i);
                 String selectedString="";

                 if (item.getItemCategory().getId().equals(ic.getId())){
                     selectedString="selected";

                 }
          %>
             <option value="<%=ic.getId() %>" <%=selectedString %>><%=ic.getName() %></option>
          <%
              }
          %>
      </select>
</span>
   
    2)使用循环迭代器方式
        <select name="category" class="select1" id="category">
           <%
              for (Iterator iter= itemCategoryList.iterator();iter.hasNext();){
                  ItemCategory ic =(ItemCategory)iter.next();
                    String selectedString="";

                 if (item.getItemCategory().getId().equals(ic.getId())){
                     selectedString="selected";

                 }
          %>    
             <option value="<%=ic.getId() %>" <%=selectedString %>><%=ic.getName() %></option>
          <%
              }
          %>
      </select>
   
    3)使用JSTL循环表达式方式(此方式不需要前提0,通过EL表达式
${ itemCategoryList }可以直接取出request中的属性值 ):  
        <select name="category" class="select1" id="category">
           <c:choose>
                <c:when test="${empty itemCategoryList}">
                    <tr>
                        <td colspan="3">没有符合条件的数据</td>
                    </tr>
                </c:when>
                <c:otherwise>
                    <c:forEach items="${itemCategoryList}" var="itemCategoryList">
                        <tr>
                            <td>${itemCategoryList.name }</td>
                        </tr>
                    </c:forEach>
                </c:otherwise>

            </c:choose>        

        </select>
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值