【问题标题】:Unable to show database table values using dataTable无法使用 dataTable 显示数据库表值
【发布时间】:2011-05-09 08:09:36
【问题描述】:

我有一个 bean 类员工具有属性 id、name 以及公共 getter 和 setter。

我正在使用以下 bean 进行数据库连接并从数据库表中获取值。

TableBean.java:

  public class TableBean{
    public Connection getVConnection() throws Exception{
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:globldb3";
      String username = "scott";
      String password = "tiger";
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username, password);
      return conn;
      }    
    public List<Employee> getPerInfoAll() {
    int i = 0;
    Connection conn = null;
    PreparedStatement pstmt = null;
    List<Employee> perInfoAll = new ArrayList(); 
     try {
        conn = getVConnection();
            String query = "select * from employee where e_id>5400";
            pstmt = conn.prepareStatement(query);
            rs = pstmt.executeQuery();
            while(rs.next()){
           System.out.println(rs.getString(1));
               perInfoAll.add(i,newEmployee(rs.getInt(1),rs.getString(2)));
               i++;
             }
             pstmt.close();
             rs.close();
             conn.close();    
      }catch (Exception e){
           e.printStackTrace();
      }
     return perInfoAll;
     }

以下是jsf页面:

 <h:dataTable value="#{TableBean.perInfoAll}" var="e">
        <h:column>
            <f:facet name="header">Employee id</f:facet>
            <h:outputText value="#{e.id}">
        </h:column>

        <h:column>
            <f:facet name="header">Employee name</f:facet>
                     <h:outputText value="#{e.name}">
        </h:column>
    </h:dataTable>

请回复。 提前致谢。

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    我认为可能是(again)getter/setter 方法命名的问题。如果您的属性被命名

    private List<Employee> perInfoAll
    

    getter 方法必须是

    public List<Employee> getPerInfoAll() { ... }
    

    注意方法名称中的大写“P”。

    此外,您的 facelet 中的 el 表达式之后不需要分号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2010-11-27
      • 2015-06-26
      • 2019-02-06
      相关资源
      最近更新 更多