java 将 arraylist 值绑定到 JSP 下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3234508/
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
Bind arraylist value to JSP dropdown list
提问by LaknathR
I have a arraylist with some data. I need to bind that data to a dropdown list in JSP and get the selected index. Can any one help me for this?
我有一个包含一些数据的数组列表。我需要将该数据绑定到 JSP 中的下拉列表并获取所选索引。任何人都可以帮助我吗?
回答by BalusC
You can use JSTLc:forEachto iterate over a list (if not done yet, just drop jstl-1.2.jarin /WEB-INF/libto install it).
您可以使用JSTLc:forEach遍历列表(如果还没有完成,只需将jstl-1.2.jar放入其中/WEB-INF/lib进行安装)。
<select name="item">
<c:forEach items="${list}" var="item">
<option value="${item.value}">${item.label}</option>
</c:forEach>
</select>
This assumes that you've a List<Item>where Itemlook like this:
这假设您的List<Item>位置Item如下所示:
public class Item {
private String value;
private String label;
// Add/generate c'tors, getters and setters.
}
In the server side you can obtain the selected item as follows:
在服务器端,您可以按如下方式获取所选项目:
String selectedItem = request.getParameter("item");
You can alternatively also use a Map<String, String>instead of a List<Item>where Itemis actually a key-value pair. You can then iterate over it the following way:
您也可以使用 aMap<String, String>而不是 a List<Item>whereItem实际上是一个键值对。然后,您可以通过以下方式对其进行迭代:
<select name="item">
<c:forEach items="${map}" var="entry">
<option value="${entry.key}">${entry.value}</option>
</c:forEach>
</select>
Related answers:
相关回答:
Update:As per the comments, you should nevercopy server-specific libraries like Tomcat/lib/*.*into the webapp's /WEB-INF/libor anywhere else in (default) runtime classpath (e.g. JRE/lib/ext). This would only lead to collisions in the classpath which leads to this kind of errors and it will make your webapp unrunnable and unportable. You should keep the server-specific libraries at their default location. Cleanup the /WEB-INF/libfrom any server-specific libraries.
更新:根据评论,您永远不应该将特定Tomcat/lib/*.*于服务器的库复制到 web 应用程序/WEB-INF/lib或(默认)运行时类路径(例如JRE/lib/ext)中的任何其他位置。这只会导致类路径中的冲突,从而导致此类错误,并且会使您的 web 应用程序无法运行和不可移植。您应该将特定于服务器的库保留在其默认位置。/WEB-INF/lib从任何特定于服务器的库中清除。
You probably copied server-specific libraries there because you wasn't able to compile your servlets. Copying the libraries in /WEB-INF/libis the wrong solution. You should basically just specify those libraries in the compiletime classpath. It's unclear which IDE you're using, but if it's Eclipse, this can be done easily: first add the server in Serversview, then associate your dynamic webapp project with the added server. On a brand new web project you can choose the server during project creation wizard. On existing web projects, you can modify it in Targeted Runtimessection in project's properties. This way Eclipse will automatically add the server-specific libraries to the project's buildpath.
您可能在那里复制了特定于服务器的库,因为您无法编译 servlet。复制库/WEB-INF/lib是错误的解决方案。您基本上应该只在编译时类路径中指定这些库。不清楚您使用的是哪个 IDE,但如果是 Eclipse,则可以轻松完成此操作:首先在Servers视图中添加服务器,然后将您的动态 webapp 项目与添加的服务器相关联。在全新的 Web 项目上,您可以在项目创建向导期间选择服务器。在现有的 Web 项目上,您可以在项目属性的Targeted Runtimes部分修改它。这样,Eclipse 会自动将特定于服务器的库添加到项目的构建路径中。

