首先导入两个selectItem的包:
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
定义一个List对象:
private List condiments = null;
定义之后是获取数据库中某一列的值:
public List getCondiments() {
try{
rs=Conn.Query("select fare from flight");
while(rs.next()){
fare=rs.getString("fare");
if(condiments == null)
{
condiments = new ArrayList();
for(int i=0;rs.getString("fare")!=null;i++)
{
condiments.add(new SelectItem(fare, rs.getString("fare")));
//往SelectOneMenu中添加SelectItems。
rs.next(); //移向数据库的下一记录。
}
}
}
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return condiments;
}
在网页中的代码:
.......
<h:selectOneMenu value="#{operationBean.condiments}"style="width: 120px">
<f:selectItems value="#{operationBean.condiments}"/>
</h:selectOneMenu>
.......
try{
rs=Conn.Query("select fare from flight");
while(rs.next()){
fare=rs.getString("fare");
if(condiments == null)
{
condiments = new ArrayList();
for(int i=0;rs.getString("fare")!=null;i++)
{
condiments.add(new SelectItem(fare, rs.getString("fare")));
//往SelectOneMenu中添加SelectItems。
rs.next(); //移向数据库的下一记录。
}
}
}
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return condiments;
}
在网页中的代码:
.......
<h:selectOneMenu value="#{operationBean.condiments}"style="width: 120px">
<f:selectItems value="#{operationBean.condiments}"/>
</h:selectOneMenu>
.......
运行效果图 :
