于 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>

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4795976.html