我尝试了,这是我的尝试。请帮助我, 我需要从数据库中填充JSP中的组合框
1-jsp页
<p class="ppp">Category</p> <jsp:useBean id="categorys" class="database.conDB" scope="page"/> <select name="categorys" size="1" style="width:196px; padding:5px;" > <c:forEach var="categ" items="${categorys.categoryNames} "> <option value="${categ}">${categ} </option> </c:forEach> </select>
=========== 2-这是conDB类中的my函数
public String[] getCategory() { String query="SELECT category_name FROM books.category;"; Statement statement; ResultSet resultSet ; try{ statement=createConnection().createStatement(); resultSet=statement.executeQuery(query); int count=0; while(resultSet.next()) count++; categoryNames=new String[count]; resultSet.first(); do{ categoryNames[count -1]=resultSet.getString("category_name"); count--; }while(resultSet.next()); }catch(Exception e) {e.printStackTrace();} for (int i = categoryNames.length; i > 0; i--) System.out.println(categoryNames[i - 1]); return categoryNames; }
您必须将selected属性添加到<option>要选择的标签中。
selected
<option>
例如:
<option value="${categ}" ${ <<selected condition>> ? 'selected' : ''>${categ}</option>
<<selected condition>>是用于确定是否应选择类别的条件。例如,如果要始终选择命名的类别house,则为:categ == 'house'
<<selected condition>>
house
categ == 'house'