簡體   English   中英

如何將兩個不同的ArrayList從Servlet發送到JSP

[英]How to send two different ArrayList from a Servlet to a JSP

我將嘗試在幾行中弄清楚,僅顯示基本代碼。 就像我在標題中所說的那樣,我必須將兩個數組列表從servlet發送到JSP頁面。 不幸的是,我不知道如何向JSP發送多個數組列表,但是我只知道發送一個數組列表。 我通常使用以下過程:

ArrayList<ClassA> array_A = new ArrayList<ClassA>();

// [...] After some operations and have filled the array_A with objects of class ClassA

ServletContext sc = request.getSession().getServletContext();
request.setAttribute("Attribute", array_A);
RequestDispatcher rd = sc.getRequestDispatcher("/MyJSP.jsp");
rd.forward(request,response);
request.getSession().removeAttribute("Attribute");

此過程使我可以將一個arraylist發送到JSP頁面。 如何發送兩個不同的arraylist? 例如,我需要發送這兩個arraylist:

ArrayList<ClassA> array_A = new ArrayList<ClassA>();
ArrayList<ClassB> array_B = new ArrayList<ClassB>();

// [...] After some operations and have filled array_A with objects of class ClassA 
// & array_B with objects of class ClassB

如何將這兩個數組從servlet發送到JSP頁面? 您能和我分享代碼嗎? 如果我不那么嚴格,我很抱歉。

request.setAttribute("array_A", array_A);
request.setAttribute("array_B", array_B);

jsp (使用jstl核心庫時,使用前綴c

<c:forEach var="itemA" items="${array_A}">
      <!-- some code here -->
</c:forEach>

<c:forEach var="itemB" items="${array_B}">
      <!-- some code here -->
</c:forEach>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM