A problem in h:dataTable implementation

依然范特西╮ 提交于 2019-12-13 03:17:32

问题


I am unable to get table values by using h:dataTable.

Please reply?

Bean class:-

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

Thanks in advance.


回答1:


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

}

With

<h:inputText value="#{employee.id}" />

and

<h:dataTable value="#{bean.employees}" var="employee">
    <h:column>#{employee.id}</h:column>
    <h:column>#{employee.name}</h:column>
</h:dataTable>

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.

Don't forget to alter your JDBC code accordingly

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

and

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

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

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



来源:https://stackoverflow.com/questions/5931360/a-problem-in-hdatatable-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!