gpt4 book ai didi

java - 如何将两个不同的 ArrayList 从 Servlet 发送到 JSP

转载 作者:行者123 更新时间:2023-11-30 08:06:17 30 4
gpt4 key购买 nike

我会尽量用几行来表达清楚,只显示基本代码。正如我在标题中所说,我必须将两个数组列表从 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");

此过程允许我将一个数组列表发送到 JSP 页面。如何发送两个不同的数组列表?例如,我需要发送这两个数组列表:

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>

关于java - 如何将两个不同的 ArrayList 从 Servlet 发送到 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34484416/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com