forEach簡單實例

常規寫法

<%
   response.setCharacterEncoding("GB2312");
   ArrayList student = (ArrayList)request.getAttribute("list");
   int size = student.size();
   for(int i = 0;i<size;i++)
   {
    Student stu = (Student)student.get(i);
    out.println("SID="+stu.getSid());
    out.println("SName="+stu.getSname());
    out.println("Score="+stu.getScore());
   } 
%>


將以上代碼片段用JSTL改寫:

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>
   <table>
    <c:forEach var="stu" items="${list}">
     <tr>
      <td>
       SID=${stu.sid}
      </td>
      <td>
       SName=${stu.sname}
      </td>
      <td>
       Score=${stu.score}
      </td>
     </tr>
    </c:forEach>
   </table>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章