怎样使用JSTL c:forEache遍历输出一个List中的List?

解决方案 »

  1.   

    jstl核心库的用法
     
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
    <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
    <title>测试jstl核心库</title>
    </head>
    <body>
    <h1>测试jstl核心库</h1>
    <hr>
    <li>测试c:out</li><br>
    hello(default):<c:out value="${hello}"/><br>
    hello(el表达式):${hello }<br>
    hello(default="123"):<c:out value="${abc}" default="123"/><br>
    hello(default="123"):<c:out value="${abc}">123</c:out><br>
    bj(defalut):<c:out value="${bj}"/><br>
    bj(escapeXml="true"):<c:out value="${bj}" escapeXml="true"/><br>
    bj(escapeXml="false"):<c:out value="${bj}" escapeXml="false"/><br>
    bj(el表达式):${bj }<br>
    <p>
    <li>测试c:set和c:remove</li><br>
    <c:set value="123" var="temp"/>
    temp:${temp }<br>
    <c:remove var="temp"/>
    temp:${temp }<br>
    <p>
    <li>测试条件控制标签c:if</li><br>
    <c:if test="${v1 lt v2}" var="v">
    v1小于v2<br>v=${v }<br>
    </c:if>
    <c:if test="${empty v3}">
    v3为空<br>
    </c:if>
    <c:if test="${empty v4}">
    v4为空<br>
    </c:if>
    <p>
    <li>测试条件控制标签c:choose,c:when,c:otherwise</li><br>
    <c:choose>
    <c:when test="${v1 lt v2}">
    v1小于v2<br>
    </c:when>
    <c:otherwise>
    v1大于v2<br>
    </c:otherwise>
    </c:choose>
    <c:choose>
    <c:when test="${empty v4}">
    v4为空<br>
    </c:when>
    <c:otherwise>
    v4不为空<br>
    </c:otherwise>
    </c:choose>
    <p>
    <li>测试循环控制标签c:forEach</li><br>
    <table border="1">
    <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>所属组</td>
    </tr>
    <c:choose>
    <c:when test="${empty userlist}">
    <tr>
    <td colspan="3">没有符合条件的数据!</td>
    </tr>
    </c:when>
    <c:otherwise>
    <c:forEach items="${userlist}" var="u">
    <tr>
    <td>${u.username }</td>
    <td>${u.age }</td>
    <td>${u.group.name }</td>
    </tr>
    </c:forEach>
    </c:otherwise>
    </c:choose>
    </table>
    <p>
    <li>测试循环控制标签c:forEach,varstatus</li><br>
    <table border="1">
    <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>所属组</td>
    </tr>
    <c:choose>
    <c:when test="${empty userlist}">
    <tr>
    <td colspan="3">没有符合条件的数据!</td>
    </tr>
    </c:when>
    <c:otherwise>
    <c:forEach items="${userlist}" var="user" varStatus="vs">
    <c:choose>
    <c:when test="${vs.count % 2 == 0}">
    <tr bgcolor="red">
    </c:when>
    <c:otherwise>
    <tr>
    </c:otherwise>
    </c:choose>
    <td>
    <c:out value="${user.username}"/>
    </td>
    <td>
    <c:out value="${user.age}"/>
    </td>
    <td>
    <c:out value="${user.group.name}"/>
    </td>
    </tr>
    </c:forEach>
    </c:otherwise>
    </c:choose>
    </table>
    <p>
    <li>测试循环控制标签c:forEach,begin,end,step</li><br>
    <table border="1">
    <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>所属组</td>
    </tr>
    <c:choose>
    <c:when test="${empty userlist}">
    <tr>
    <td colspan="3">没有符合条件的数据!</td>
    </tr>
    </c:when>
    <c:otherwise>
    <c:forEach items="${userlist}" var="user" begin="2" end="8" step="2">
    <tr>
    <td>${user.username}</td>
    <td>${user.age}</td>
    <td>${user.group.name }</td>
    </tr>
    </c:forEach>
    </c:otherwise>
    </c:choose>
    </table>
    <p>
    <li>测试循环控制标签c:forEach,普通循环</li><br>
    <c:forEach begin="1" end="10">
    a<br>
    </c:forEach>
    <p>
    <li>测试循环控制标签c:forEach,输出map</li><br>
    <c:forEach  items="${mapvalue}" var="v">
    ${v.key }=${v.value }<br>
    </c:forEach>
    <p>
    <li>测试循环控制标签c:forTokens</li><br>
    <c:forTokens items="${strTokens}" delims="," var="v">
    ${v }<br>
    </c:forTokens>
    <p>
    <li>测试c:catch</li><br>
    <%
    try {
    Integer.parseInt("asdfsdf");
    }catch(Exception e) {
    out.println(e.getMessage());
    }
    %>
    <p>
    <c:catch var="exinfo">
    <%
    Integer.parseInt("asdfsdf");
    %>
    </c:catch>
    ${exinfo }<br>
    <p>
    <li>测试c:import</li><br>
    <c:import url="http://localhost:8080/struts_login"/>
    <p>
    <li>测试c:url和c:param</li><br>
    <c:url value="http://localhost:8080/drp/sysmgr/user_add.jsp" var="v">
    <c:param name="username" value="Jack"/>
    <c:param name="age" value="20"/>
    </c:url>
    ${v }<br>
    <li>测试:redirect</li><br>
    <c:redirect context="/struts_login" url="/index.jsp"/>
    </html>
      

  2.   


    <c:forEach var="fligth" items="${fligthList}">
        <tr>
        <td>${fligth.corporationId }</td>
        <td>${fligth.fligthNo }</td>
        <td>${fligth.fligthTime }</td>
        <td>${fligth.corporationName }</td>
        </tr>
        </c:forEach>
    给你个例子
      

  3.   

    这样的呀,不用MVC 写么?
      

  4.   

    <c:forEach items="${users}" var="user">
    <b>${user.name}</b>
    </c:forEach>
    items的值users是传过来的一个集合,var的值user是这个集合的元素
      

  5.   

    我也是刚刚学习jstl   不过zl的是真确的
      

  6.   

    问题解决啦,至于要到MVC 在后台request.setAttribute("list",list);这是一个集合
    在界面用jstl就简单了
    <c:forEach items="${list}" var="l">
       <b>${l.XX}</b>//xx就是你这个集合中的属性了.
    </c:forEach>
      

  7.   

    <c:forEach items="${users}" var="user">
    <b>${user.name}</b>
    </c:forEach>items属性里是保存在作用域里面的集合,var表示给这个集合去一个别名,