日期:2014-05-18  浏览次数:20756 次

一个关于jstl中<c:forEach的问题
<%@   page   language= "java "   import= "java.util.* "   pageEncoding= "UTF-8 "%>
<%@   taglib   prefix= "c "   uri= "/WEB-INF/c.tld "   %>
<%@   taglib   prefix= "c1 "   uri= "/WEB-INF/c-1_0.tld "   %>
<%@   taglib   prefix= "c2 "   uri= "/WEB-INF/c-1_0-rt.tld "   %>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
    <head>
     
        <title> jstl简单测试 </title>
       
    </head>
   
    <body>
    <%
    String[]   strsList={ "1111 ", "22222 ", "33333 "};
    String[]   tList={ "a ", "b ", "c "};
    %>
    <table   width= "300 "   border= "1 ">
    <c:forEach   var= "strs "   items= "${tList} ">
    <tr>
    <td> ${strs} </td>
    </tr>
    </c:forEach>
    </table>
    <table   width= "300 "   border= "1 ">
    <c:forEach   var= "x "   begin= "0 "   end= "10 "   step= "2 ">
    <tr>
    <td> <c:out   value= "${x} "/> </td>  
    <td> <c:out   value= "${x   *   x} "/> </td>
    </tr>
    </c:forEach>
    </table>
    </body>
</html>

我的引入是对的,为什么下面输出数字是正常的,而上面我输出数组时却没有效果(无输出,也不提示错误).

------解决方案--------------------
你需要把数据添加到 page 或者 request 中就可以了。
------解决方案--------------------
<%
String[] tList={ "a ", "b ", "c "};
request.setAttribute( "tList ", tList);
%>
<table width= "300 " border= "1 ">
<c:forEach var= "strs " items= "${tList} ">
<tr>
<td> ${strs} </td>
</tr>
</c:forEach>
</table>