开发者

Java populating <option> dropdown list

开发者 https://www.devze.com 2023-03-03 03:02 出处:网络
I have the following code: <div> <% TaxonomicTypeFeed ttf = new TaxonomicTypeFeed(); ArrayList<String> tmp = ttf.getTypes();

I have the following code:

<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));
    }开发者_C百科
%>
            
    <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.


You need to print the values by <%= %>. The <% %> won't print anything.

<option value="<%=str%>"><%=str%></option>

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>

Instead of <jsp:useBean> you can also use a preprocessing servlet.

0

精彩评论

暂无评论...
验证码 换一张
取 消