java 将 ArrayList 从 Servlet 传递到 JSP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:01:57  来源:igfitidea点击:

Passing an ArrayList from a Servlet to JSP

javamodel-view-controllerjspservletsjstl

提问by Dhruv Gairola

my model returns an arraylist of strings to servlet in the form

我的模型以以下形式向 servlet 返回一个字符串数组列表

ArrayList<String> currentCustomer = model.getAllCustomers();

i want to pass this arraylist from the servlet to the jsp page. how do i do this? below is what i tried

我想将此数组列表从 servlet 传递到 jsp 页面。我该怎么做呢?下面是我试过的

req.setAttribute("currentCustomer", currentCustomer);

and in the jsp page, i want to use JSTL to loop over each value and display it. how do i do that? its frustrating me to no end. ive scoured the web but to no avail. any help is greatly appreciated.

在jsp页面中,我想使用JSTL来遍历每个值并显示它。我怎么做?它让我无休止地沮丧。我在网上搜索但无济于事。任何帮助是极大的赞赏。

here is the jsp code

这是jsp代码

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>


<body>
    <div>
        <c:forEach var="customer" items="currentCustomer">
            ${customer}
        </c:forEach>
    </div>
</body>

回答by Filip Spiridonov

Let's make it work :)

让我们让它工作:)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="customer" items="${currentCustomer}">
     <c:out value="${customer.name}" />
     <c:out value="${customer.age}" />
</c:forEach>

P.S. jsp:useBean is another way to go...

PS jsp:useBean 是另一种方法......

P.P.S. I also made a correction in the taglib import. That's one of these hard-visible mistakes when you can look on two different entries and think they are the same :)

PPS 我还在 taglib 导入中进行了更正。当您可以查看两个不同的条目并认为它们相同时,这是这些明显的错误之一:)

回答by Dhruv Gairola

its allrite guys, i solved the problem.. thanks for your help..

它的 allrite 家伙,我解决了问题.. 感谢您的帮助..

apparently the code i was using was outdated (thanks internet!) i was writing this on the header:

显然我使用的代码已经过时(感谢互联网!)我在标题上写了这个:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

while it should have been

虽然它应该是

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

回答by Stan Kurilin

It will be smt like

它会像 smt

<c:forEach var="currentCustomer" items="${customers}">
     ${currentCustomer.name}
     ${currentCustomer.age}
</c:forEach>