h:dataTable实现中的问题 [英] A problem in h:dataTable implementation

查看:88
本文介绍了h:dataTable实现中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am unable to get table values by using 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"/>

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

谢谢.

推荐答案

您需要提供 normal getter和setter方法. EL不会按字段访问属性,它将仅通过getter访问属性,而仅通过setter对其进行更改.如果您懒于键入,甚至可以让您的IDE(例如Eclipse/Netbeans/IntelliJ)自动生成它们.

You need to provide normal getter and setter methods. EL won't access properties by fields, it will access them by getters only and mutate them by setters only. If you're lazy in typing, you can even let your IDE (such as Eclipse/Netbeans/IntelliJ) autogenerate them.

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规范规则. #{employee.id}需要一个public Object getId()方法(其中Object是您想要的任何类型).在您的示例中,您有#{employee.eId}可能是无效的.如果标识符public Object getEId()的前两个字符是大写的,则应通过#{employee.EId}访问它.但是毕竟,使用匈牙利语或前缀表示法没有任何意义.只是摆脱e前缀.这也使代码也更具自记录性.

Please note that it is case sensitive and should follow Javabeans spec rules. The #{employee.id} expects a public Object getId() method (where Object is whatever type you want it to be). In your example you have #{employee.eId} which can impossibly be valid. If the first two chars of the identifier public Object getEId() are uppercased, you should access it by #{employee.EId}. But after all, using Hungarian or prefix notations makes no sense. Just get rid of the e prefix. It makes code also more self-documenting.

别忘了相应地更改JDBC代码

Don't forget to alter your JDBC code accordingly

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

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

请注意,传递ResultSet作为方法的返回值是一个坏主意.一旦将其关闭再返回,就无法再从中获取值了.而当您不关闭它时,则会泄漏资源.您需要在与打开和关闭它的方法块相同的方法块中对其进行处理.在上面的示例中,只需执行return employee;.

Note that passing ResultSet around as method return value is a bad idea. Once you close it before returning, you cannot get values from it anymore. And when you don't close it, then you're leaking resources. You need to process it inside the very same method block as where you've opened and closed it. In the above example, just do return employee;.

要了解如何开始使用基本DAO,请检查此文章.

To learn how to get started with basic DAO, check this article.

这篇关于h:dataTable实现中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆