Jsf h:dataTable实现中的一个问题

Jsf h:dataTable实现中的一个问题,jsf,jsf-2,Jsf,Jsf 2,我无法使用h:dataTable获取表值 请回答 Bean类:- public class Employee implements Serializable{ private int eId; private String eName; private ResultSet viewEmployee; public Connection getVConnection() throws Exception{ Stri

我无法使用h:dataTable获取表值

请回答

Bean类:-

 public class Employee implements Serializable{
        private int eId;
        private String eName; 
        private ResultSet viewEmployee;
    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 ResultSet getViewEmployee()  throws Exception{
            Connection conn = null;
            PreparedStatement pstmt = null;
            try {
              conn = getVConnection();
              String query = "select * from employee where e_id>?";
              pstmt = conn.prepareStatement(query); // create a statement
              pstmt.setInt(1,this.eId); // set input parameter
              result = pstmt.executeQuery();
                return result;
            }finally {
              try {
                result.close();
                pstmt.close();
                conn.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
        }
    public String v2(){
        try{
            getViewEmployee();
            return "view-employee-p.xhtml";
        }catch(Exception e){
            e.printStackTrace();
            return "home.xhtml";
         }
    }
JSF第1页:-

<h:panelGrid columns="3" cellpadding="2" border="2" styleClass="panelGridColums">           Id   
<h:inputText id="id" label="&#160;"  value="#{employee.eId}" 
    required="true" requiredMessage="Field cannot be left blank"
    converterMessage="Not a valid id, id must be betwenn 1 and 9999." maxlength="4">
    <f:convertNumber/>      
</h:inputText>      
 <h:message for="id" styleClass="errorMessages" showDetail="false" showSummary="true"/>
 </h:panelGrid>
<h:commandButton value="View" action="view-page.xhtml"/>
Id
查看页面。xhtml:

      <h:dataTable value="#{employee.viewEmployee}" var="e">
<h:column>
    <f:facet name="header">Employee id</f:facet>
    #{e.E_ID};
</h:column>

<h:column>
    <f:facet name="header">Employee id</f:facet>
    #{e.E_Name};
</h:column>
</h:dataTable>

员工id
#{e.e_ID};
员工id
#{e.e_Name};

提前感谢。

您需要提供正常的getter和setter方法。EL不会通过字段访问属性,它将仅通过getter访问属性,并仅通过setter对属性进行修改。如果你懒于打字,你甚至可以让你的IDE(比如Eclipse/Netbeans/IntelliJ)自动生成它们

public class Employee {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

请注意,将
ResultSet
作为方法返回值传递是个坏主意。一旦在返回之前关闭它,就无法再从中获取值。如果你不关闭它,那么你就是在泄漏资源。您需要在打开和关闭它的同一个方法块中处理它。在上面的例子中,只需做
returnemployee


要了解如何开始使用基本DAO,请检查。

您能否停止在标题中添加“一个非常小的问题”或类似的内容?乍一看,它并没有清楚地描述具体问题。我已经编辑过好几次了。请用一个清晰的标题,用一句话概括具体问题,然后单击
编辑
链接。@BalusC:好的,谢谢你这么好的建议。谢谢。你能给我一些关于列表方法实现的提示吗,在你的回答中提到了。让它返回列表就行了。在构造函数或后构造函数中执行预加载作业。
<h:inputText value="#{employee.id}" />
<h:dataTable value="#{bean.employees}" var="employee">
    <h:column>#{employee.id}</h:column>
    <h:column>#{employee.name}</h:column>
</h:dataTable>
preparedStatement.setInt(1, employee.getId());
Employee employee = new Employee();
employee.setId(resultSet.getInt("id"));
employee.setName(resultSet.getString("name"));