java 如何在 JSP 中创建对象的数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11843853/
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
how to create an array list of objects in JSP
提问by user1581636
I want to create an arraylist of objects in JSP. And after that, want to loop through the list objects. can some one please help me in creating it.
我想在 JSP 中创建一个对象数组列表。之后,想要遍历列表对象。有人可以帮我创建它吗?
回答by Jigar Joshi
Create the ArrayList
at servlet set it as attribute, and iterate it on JSP using <c:forEach>
创建ArrayList
at servlet 将其设置为属性,并使用在 JSP 上对其进行迭代<c:forEach>
Servlet
小服务程序
List<Foo> list = new ArrayList<Foo>();
list.add(foo1);
list.add(foo2);
list.add(foo3);
request.setAttaribute("fooList", list);
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
hello.jsp
你好.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach items="${list}" var="foo">
<tr>
<td><c:out value="${foo.name}" /></td>
<td><c:out value="${foo.age}" /></td>
</tr>
</c:forEach>
Note: name
and age
are two properties of Foo
with proper accessor methods
注意:name
和age
是Foo
具有适当访问器方法的两个属性