Java填充<选项>下拉列表 [英] Java populating <option> dropdown list
本文介绍了Java填充<选项>下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
<div>
<%
TaxonomicTypeFeed ttf = new TaxonomicTypeFeed();
ArrayList<String> tmp = ttf.getTypes();
System.out.println("Going to print");
for (int i=0; i < tmp.size(); i++)
{
System.out.println(tmp.get(i));
}
%>
<form>
<select>
<%
Iterator<String> i = tmp.iterator();
while (i.hasNext())
{
String str = i.next(); %>
<option value="<%str.toString();%>"><%str.toString();%>
</option>
<%}%>
</select>
</form>
</div>
它创建一个下拉列表,但没有文字。
这是我以前第一次使用任何这些选项,所以我不知道我是否以正确的方式进行,
It creates a dropdown list fine, however there is no text. This is the first time I have ever used any of these options before, so I have no idea if I am even going about it in the right manner,
U。
推荐答案
您需要以<%=%>
。 <%%>
不会打印任何内容。
You need to print the values by <%= %>
. The <% %>
won't print anything.
<option value="<%=str%>"><%=str%></option>
无关问题:这是不是最佳做法。考虑使用taglibs / EL。您最终可以获得更好的可读/可维护的代码。
Unrelated to the problem: this is not the best practice. Consider using taglibs/EL. You end up with better readable/maintainable code.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="taxonomicTypeFeed" class="com.example.TaxonomicTypeFeed" />
...
<select>
<c:forEach items="${taxonomicTypeFeed.types}" var="type">
<option value="${type}">${type}</option>
</c:forEach>
</select>
而不是< jsp:useBean>
您还可以使用预处理 servlet 。
Instead of <jsp:useBean>
you can also use a preprocessing servlet.
这篇关于Java填充<选项>下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文