h:dataTable实现中的一个问题

时间:2011-05-09 00:32:57

标签: 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{
            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"/>

查看-page.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>

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要提供普通 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;
    }

}

使用

<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>

请注意,它区分大小写,应遵循Javabeans spec规则。 #{employee.id}需要public Object getId()方法(其中Object是您希望的类型)。在您的示例中,您#{employee.eId}可能无效。如果标识符public Object getEId()的前两个字符是大写的,则应通过#{employee.EId}访问它。但毕竟使用匈牙利语或前缀符号是没有意义的。只需摆脱e前缀。它使代码也更加自我记录。

不要忘记相应地更改JDBC代码

preparedStatement.setInt(1, employee.getId());

Employee employee = new Employee();
employee.setId(resultSet.getInt("id"));
employee.setName(resultSet.getString("name"));

请注意,将ResultSet作为方法返回值传递是一个坏主意。一旦在返回之前关闭它,就不能再从中获取值。当你不关闭它,那么你就是在泄漏资源。您需要在与打开和关闭它的方法块相同的方法块内处理它。在上面的示例中,只需执行return employee;

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