如何在jsp中填充组合框? [英] how to fill combobox in jsp?
本文介绍了如何在jsp中填充组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了,这是我的尝试.请帮助我 我需要从数据库中的jsp中填充组合框
I tried and this is my attempt Please help me i need to fill combobox in jsp from my database
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类中的功能
=========== 2-this my function in conDB class
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>
标签中.
You have to add the selected
attribute to the <option>
tag you want to select.
例如:
<option value="${categ}" ${ <<selected condition>> ? 'selected' : ''>${categ}</option>
<<selected condition>>
是用于确定是否应选择类别的条件.例如.如果要始终选择名为house
的类别,则为:categ == 'house'
<<selected condition>>
is the condition that is used to determine if the category should be selected. E.g. If you want to always select the category named house
it would be: categ == 'house'
这篇关于如何在jsp中填充组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文