在 jsp页面中遍历list中的数据
往往我们都会将查询到的数据显示到界面中,那么该如何在界面显示,请看下面的详解:
0)前提得在jsp页面中获取后台传过来的数据(在此为List集合):
1)使用for循环方式:
2)使用循环迭代器方式:
3)使用JSTL循环表达式方式(此方式不需要前提0,通过EL表达式${itemCategoryList}可以直接取出request中的属性值):
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>
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
