开发者

Passing an ArrayList from a Servlet to JSP

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

ArrayList&l开发者_如何学Pythont;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

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.

here is the jsp code

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


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


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...

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 :)


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

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" %>


It will be smt like

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜