java 在jsp的下拉列表中显示数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13032507/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:15:30  来源:igfitidea点击:

Displaying list of array in drop down in jsp

javajsp

提问by Rachel

I have array populated with elements and I want to display elements of that array in drop down list.

我用元素填充了数组,我想在下拉列表中显示该数组的元素。

Here is relevant piece of code.

这是相关的一段代码。

 Party[] Parties = party.getAllParties;

In my jsp page, i have

在我的jsp页面中,我有

 <td nowrap>
    <select label="Party List" array="Parties" name="Party List">
        <option value=<%= (Parties) %>></option>
    </select>
</td>

Now when i go and check view source of jsp page, i have

现在当我去检查jsp页面的查看源时,我有

<td nowrap>
  <select label="Party List" array="Parties" name="Party List">
     <option value=[Lcom.areil.pdo.party.Party;@1404de3></option>
  </select>
</td>

I know, way option value is set is not correct and am not sure what is right way of doing it.

我知道,设置选项值的方式不正确,我不确定这样做的正确方法是什么。

采纳答案by kosa

Yes, it is because you are trying to display array directly.

是的,这是因为您试图直接显示数组。

The statement you have is equal to System.out.println(Parties);

你的陈述等于 System.out.println(Parties);

You need to loop through the array and display each element by index like parties[i].

您需要遍历数组并按索引显示每个元素,如parties[i]

Example:

例子:

for(int i=0;i<Parties.length;i++) 
{ %> 
<tr><td><%=Parties[i]%></td></tr><% 
} 

回答by zaffargachal

Place this code:

放置此代码:

<c:forEach var="party" items="${Parties}">
    <option value="${party}" />
</c:forEach>

回答by Paulius Matulionis

What you want to do is to perform the JSTL forEachstatement into your JSPand to output your values with the EL. Try to use as less code of scriptlets as you can, the EL was design to replace scriplets.

您想要做的是将JSTL forEach语句执行到您的JSP并使用EL输出您的值。尽量使用尽可能少的脚本代码,EL 旨在取代脚本。

Include this:

包括这个:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

into your JSP to be able to use JSTL. So the code of your proper JSP should look like this:

进入您的 JSP 以便能够使用JSTL. 所以你的正确 JSP 的代码应该是这样的:

<td>
    <select label="Party List" array="Parties" name="Party List">
        <c:forEach var="party" items="${Parties}">
            <option value="${party}">
                <c:out value="${party}"/>
            </option>
        </c:forEach>
    </select>
</td>

回答by user3722343

<select name="party"> 
    <option value="">SELECT</option> 
    <% for(int i=0;i<Parties.size();i++){ 
        String party= (String)Parties.get(i); %> 
        <option value="<%=party%>" > <%=party%> 
        </option>
    <%}%> 
</select>