简体   繁体   English

JSP 在下拉框中使用数组列表的内容

[英]JSP use contents of an array list in a drop down box

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 availableLines in 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!谢谢!

You can add the bellow line just after the for loop in your scriptlet:您可以在 scriptlet 中的for循环之后添加波纹管:

request.setAttribute("availableLines", availableLines);

And then you can use the availableLines variable in your drop down list using JSTL like bellow:然后您可以使用JSTL在下拉列表中使用availableLines变量,如下所示:

 <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!但是你应该知道的一件事是,编写 scriptlet 已被弃用多年! So it is advised to move your scriptlet code to your corresponding servlet .因此,建议将您的 scriptlet 代码移动到您相应的servlet Here is a step by step tutorial for Servlet and jsp for beginners. 是初学者的Servletjsp步教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM