java JSP 在下拉框中使用数组列表的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17277613/
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
JSP use contents of an array list in a drop down box
提问by laitha0
I have an arraylist
that is declared and populated in a JSP
file:
我arraylist
在JSP
文件中声明并填充了一个:
<% for(int i=0; i< GDI.getRow(); i++){
associatedLines[i] = GDI.getRow().get(i).getNumplanindex;
}
ArrayList<Integer> availableLines = new ArrayList<Integer>();
for(int i=0; i<associatedLines.length; i++){
if(associatedLines[i] == null){
availableLines.add(i);
}
}
%>
I would like to use the contents of availableLinesin the dropdown list and be able to store the value selected to be used somewhere else.
我想在下拉列表中使用availableLines的内容,并能够将选择的值存储在其他地方使用。
I am almost certain that I need to use JSTL
but I am not sure how to do it.
我几乎可以肯定我需要使用,JSTL
但我不确定如何使用。
Hopefully somebody can help. Thanks!
希望有人可以提供帮助。谢谢!
回答by Sazzadur Rahaman
You can add the bellow line just after the for
loop in your scriptlet:
您可以for
在 scriptlet 中的循环之后添加波纹管:
request.setAttribute("availableLines", availableLines);
And then you can use the availableLines
variable in your drop down list using JSTL
like bellow:
然后您可以使用availableLines
下拉列表中的变量,JSTL
如下所示:
<select>
<c:forEach var="line" items="${availableLines}">
<option><c:out value="${line}"/></option>
</c:forEach>
</select>
I think, this answers your original question.
我想,这回答了你原来的问题。
Edit:
编辑:
But one thing you should know that, writing scriptlet is deprecated for years! So it is advised to move your scriptlet code to your corresponding servlet
. Hereis a step by step tutorial for Servlet
and jsp
for beginners.
但是您应该知道的一件事是,编写 scriptlet 已被弃用多年!因此,建议将您的 scriptlet 代码移动到您相应的servlet
. 这里是一步步教程Servlet
和jsp
初学者。