JSTL的forEach標籤和Struts的logic:iterate標籤

來源:互聯網
上載者:User

用於解決Action中request.setAttribute("list",list)

由於我用的是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="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>或者<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.