由于我用的是struts框架,就拿整個項目介紹:

          1.首先把jstl的兩個常用包jstl.jar、standard.jar加載到環境中

          2.Action代碼:(整個過程不需要了解,這兒方法就是返回一個封裝Students對象的list,然后request.setAttribute("list", list)起來)

          public ActionForward selectStudent(ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response) {
             StudentForm studentForm = (StudentForm) form;
             DBConnection dbconn = new DBConnection();
             Connection conn = dbconn.getConnection();
             StudentServiceFactory serviceFactory = new StudentServiceFactory();
             List list = serviceFactory.getStudentService().selectStudent(conn);
             request.setAttribute("list", list);
             try {
              conn.close();
             } catch (SQLException e) {
              e.printStackTrace();
             }
             return mapping.findForward("show");
          }

          3.show.jsp頁面:

          <%@ page language="java" pageEncoding="utf-8"%>
          <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>//這三句很重要

          <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

          <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
              <base href="<%=basePath%>">
             
              <title>My JSP 'show.jsp' starting page</title>
             
          <meta http-equiv="pragma" content="no-cache">
          <meta http-equiv="cache-control" content="no-cache">
          <meta http-equiv="expires" content="0">   
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          <meta http-equiv="description" content="This is my page">
          <!--
          <link rel="stylesheet" type="text/css" href="styles.css">
          -->

          </head>

          <body>
              查詢結果如下: <br>
          <table>
             <tr>
              <td>ID</td>
              <td>姓名</td>
             </tr>
            <c:forEach items="${list}" var="student">// items為list的一個迭代器,list為Action中傳遞過來的list,student為Student類對象
             <tr>
             <td>${student.id }</td>//輸出student的id屬性
              <td>${student.name }</td>//輸出student的name屬性
             </tr>
             </c:forEach>

          <logic:iterate id="li" name="list" type="vo.Student">//id為自定義的名字,name為Action中傳過來的list,type為實體類,包括完整路徑,這里是vo.Student
              <tr>
               <td><bean:write name="li" property="id"/></td>//name為邏輯名,和logic:iterate id="li"中的id對應,property為實體類中真正的屬性
               <td><bean:write name="li" property="name"/></td>
               <td><a href="student.do?method=deleteStudent&id=<bean:write name="li" property="id"/>">刪除</a></td>
              </tr>
             </logic:iterate>


          </table>
          <a href="student.jsp">返回</a>
          </body>
          </html>

          在JSP頁面引入Struts標簽庫的時候有所不同:

          struts1.3的為:

          <%@ taglib uri=" <%@ taglib uri=" <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>或者<%@ taglib uri="

          Feedback

          # re: JSTL的forEach標簽和Struts的logic:iterate標簽(解決Action中request.setAttribute("list",list))  回復  更多評論   

          2009-08-04 15:04 by aguojava
          還是直接使用核心標簽庫就可以了,沒有必要和Struts耦合在一起。而且jstl寫起來也方便多。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 大同市| 茶陵县| 梅州市| 高阳县| 凤山市| 洪江市| 建水县| 黑山县| 九台市| 德化县| 宁阳县| 玉林市| 龙陵县| 贺兰县| 吉安市| 平度市| 大安市| 涞水县| 综艺| 大竹县| 抚远县| 韩城市| 惠州市| 宣化县| 建始县| 章丘市| 寻甸| 西昌市| 洮南市| 普格县| 大厂| 延津县| 太保市| 龙门县| 会宁县| 遂川县| 闽侯县| 治县。| 潼南县| 梅州市| 烟台市|