创建下拉列表以显示JSF中的数据库数据

创建下拉列表以显示JSF中的数据库数据,jsf,jakarta-ee,Jsf,Jakarta Ee,(注意:我已经检查了类似标题的问题,但没有帮助,或者我没有正确理解解决方案) 我所要做的就是从数据库中检索国家名称,并将它们显示在下拉列表中。问题来自JSF的实现,因为查询读取和连接工作正常 index.xhtml <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/D

(注意:我已经检查了类似标题的问题,但没有帮助,或者我没有正确理解解决方案)


我所要做的就是从数据库中检索国家名称,并将它们显示在下拉列表中。问题来自JSF的实现,因为查询读取和连接工作正常

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Top Ten</title>
    </h:head>
    <h:body>
        <h1>Choose a country</h1>
        <h:form>
            <h:selectOneMenu value="#{runner.getCountryList()}" >
            <f:selectItems value="#{runner.getCountryList()}"/>
            </h:selectOneMenu>
        </h:form>
    </h:body>
</html>
输出:
谢谢

结果中必须返回字符串列表,即国家名称,而不是跑步者列表。您只需迭代resultset并将国家“字符串”数据添加到列表中。您实际调用的是runner托管Bean的一个实例和来自它的getCountryList方法,它应该返回字符串列表(国家名称),而不是runner列表。

问题来自JSF的实现?请发布您面临的确切错误?@Rahussaini如果您点击最后的图像,您将看到输出。我没有得到一个错误,我要么显示错误的输出(JSF),要么检索不到正确的数据。谢谢你,但你必须明白,在办公室访问imgur、facebook、twitter等链接时不能打开/解析,因为这些链接是被禁止的。因此无法看到错误图像。您是否可以尝试删除arrayList,即创建一个虚拟arrayList并返回它,然后查看错误是否仍然存在?它必须是一个字符串列表,即在结果中返回的国家名称列表,而不是运行者列表列表。您只需迭代resultset并将国家“字符串”数据添加到列表中。您实际调用的是runner托管Bean的一个实例和来自该实例的getCountryList方法,该方法应返回字符串列表(国家名称)代替Runner列表。@Rahusaini当我在getCountryList中放置一个虚拟列表并将方法更改为“代替一切”时,效果良好,因此必须检索信息。感谢您的时间,但我无法确定如何在while循环中将项目添加到列表中。结果集结果=ps.executeQuery();列表=新的ArrayList();while(result.next()){//将所有数据存储到一个列表中。添加(???)}您在runner.setCountry(result.getString(“country”)中设置的任何内容;仅此列表。添加(result.getString(“country”)!!!!我这样做了,现在我得到了错误:无法获取数据库连接bug并检查JNDI查找是否解决了“jdbc/Honolu2017”。无论您使用什么应用服务器,都必须在Tomcat/JBoss/Glassfish等应用服务器中预先配置此JNDI名称上下文。如果JNDI成功解析,您应该拥有来自数据源的数据库连接。问题不在于JNDI,因为它可以与程序中的其他方法一起正常工作,由于某种原因,它无法使用此getCountryList方法的新实现连接到数据库
package honolulu.marathon;

/* imports have been removed to make code compact */
@ManagedBean(name="runner")
@SessionScoped
public class Results implements Serializable{
        private Connection con = null;
    private DataSource ds;
    public Results(){
        try {
                Context ctx = new InitialContext();
                ds = (DataSource)ctx.lookup("jdbc/honolulu2017");
        } catch (NamingException e) {
                e.printStackTrace();
        }   
    }
    private Connection getConnection(){
        try {
            con = ds.getConnection();
        } catch(Exception e){
            e.printStackTrace();
        }
        return con;        
    }
    /* fill the drop-down list */
    public List<Runner> getCountryList() throws SQLException{
        getConnection();
        if(ds==null)     
                throw new SQLException("Can't get data source");
        //get database connection
        if(con==null)
                throw new SQLException("Can't get database connection");
        PreparedStatement ps 
                = con.prepareStatement(
                   "select COUNTRY from RESULTS_TEST");
        //get runner data from database
        ResultSet result =  ps.executeQuery();
        List<Runner> list = new ArrayList<>();
        while(result.next()){
                Runner runner = new Runner();
                runner.setCountry(result.getString("country"));
                //store all data into a List
                list.add(runner);
        }
        return list;  
      }

}
package honolulu.marathon;

public class Runner {

    public String country;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }    
}