Java 在 JSP 中迭代 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16397207/
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
Iterate ArrayList in JSP
提问by Anish Sharma
I have two arraylists in my class and I want to send it to my JSP and then iterate the elements in arraylist in a select tag.
我的班级中有两个数组列表,我想将它发送到我的 JSP,然后在选择标记中迭代数组列表中的元素。
Here is my class:
这是我的课:
package accessData;
import java.util.ArrayList;
public class ConnectingDatabase
{
ArrayList<String> food=new ArrayList<String>();
food.add("mango");
food.add("apple");
food.add("grapes");
ArrayList<String> food_Code=new ArrayList<String>();
food.add("man");
food.add("app");
food.add("gra");
}
I want to iterate food_Code as options in select tag and food as values in Select tag in JSP; my JSP is:
我想将 food_Code 迭代为 select 标签中的选项,并将 food 迭代为 JSP 中 Select 标签中的值;我的 JSP 是:
<select id="food" name="fooditems">
// Don't know how to iterate
</select>
Any piece of code is highly appreciated. Thanks in Advance :)
任何一段代码都受到高度赞赏。提前致谢 :)
采纳答案by Prakash K
It would be better to use a java.util.Map
to store the key and values instead of two ArrayList
, like:
最好使用 ajava.util.Map
来存储键和值而不是 two ArrayList
,例如:
Map<String, String> foods = new HashMap<String, String>();
// here key stores the food codes
// and values are that which will be visible to the user in the drop-down
foods.put("man", "mango");
foods.put("app", "apple");
foods.put("gra", "grapes");
// if this is your servlet or action class having access to HttpRequest object then
httpRequest.setAttribute("foods", foods); // so that you can retrieve in JSP
Now to iterate the map in the JSP use:
现在要在 JSP 中迭代地图,请使用:
<select id="food" name="fooditems">
<c:forEach items="${foods}" var="food">
<option value="${food.key}">
${food.value}
</option>
</c:forEach>
</select>
Or without JSTL:
或者没有 JSTL:
<select id="food" name="fooditems">
<%
Map<String, String> foods = (Map<String, String>) request.getAttribute("foods");
for(Entry<String, String> food : foods.entrySet()) {
%>
<option value="<%=food.getKey()%>">
<%=food.getValue() %>
</option>
<%
}
%>
</select>
To know more about iterating with JSTL here is a good SO answerand here is a good tutorialabout how to use JSTL in general.
要了解有关使用 JSTL 进行迭代的更多信息,这里是一个很好的SO 答案,这里有一个关于如何使用 JSTL的很好的教程。
回答by Michal Borek
You can use JSTL's foreach.
您可以使用 JSTL 的 foreach。
<c:forEach items="${foodItems}" var="item">
${item}
</c:forEach>
You need also to import JSTL core:
您还需要导入 JSTL 核心:
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
回答by Madhusudan Joshi
<c:forEach items="${list}" var="foodItem">
${foodItem.propertyOfBean}
</c:forEach>
This will solve your problem.
这将解决您的问题。
回答by Santosh
There are multiple ways this can be done (with some changes in your scheme)
有多种方法可以做到这一点(在您的计划中进行一些更改)
Using JSTL:
使用 JSTL:
Create a bean with two fields as
food
andfood_code
public class Food{ private String food; private String food_code; // Setter/getters follows }
创建一个带有两个字段的 bean
food
和food_code
公共类食品{ 私人字符串食品;私人字符串 food_code; // Setter/getters 跟随 }
Now the arraylist available on the page will be list Food
objects.
In the JSP code, you can use following:
现在页面上可用的数组列表将是列表Food
对象。在 JSP 代码中,您可以使用以下内容:
<select name="fooditems">
<c:forEach items="${list}" var="item">
<option value="${item.food_code}">${item.food}</option>
</c:forEach>
</select>
If you are using struts:
如果您使用的是支柱:
<html:select property="fooditems" >
<html:optionsCollection property="list" label="food" value="food_code" />
</html:select>
Here list
is object key which will be used to retrieve the list from context (page/session)
这list
是用于从上下文(页面/会话)中检索列表的对象键
回答by venky
You can retrieve the list in JSP as
您可以在 JSP 中检索列表作为
<select id="food" name="fooditems">
<c:forEach items="${listname}" var="food" >
<option value="${food}"> ${food} </option>
</c:forEach>
</select>