Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing a parameter to a method in the dataTable's value attribute

843844Nov 21 2007 — edited Nov 22 2007
Hi,

I'm brand new to JSF, so I might be overlooking something really simple. I'm trying to use a dataTable to display data from a database. It works fine until I try to pass a parameter to "#{customer.all}". My backer bean is ready (or so I believe) and is included after the jsp code snippet. What do I need to do to the jsp/jsf code in order to pass this parameter?

jsp code:
<html>
   <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html"  prefix="h" %>
   <f:view>
      <head>
         <link href="styles.css" rel="stylesheet" type="text/css"/>
         <title>
            <h:outputText value="#{msgs.pageTitle}"/>
         </title>
      </head>
      <body>
         <h:form>
            <h:dataTable value="#{customer.all}" var="customer" 
               styleClass="customers" 
               headerClass="customersHeader" columnClasses="custid,name">
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.customerIdHeader}"/>
                  </f:facet>
                  <h:outputText value="#{customer.Cust_ID}"/>
               </h:column>
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.nameHeader}"/>
                  </f:facet>
                  <h:outputText value="#{customer.Name}"/>
               </h:column>
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.phoneHeader}"/>
                  </f:facet>
                  <h:outputText value="#{customer.Phone_Number}"/>
               </h:column>
            </h:dataTable>
         </h:form>
      </body>
   </f:view>
</html>
backer bean:
package com.corejsf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import javax.sql.DataSource;

public class CustomerBean {
   private Connection conn;
   
   public void open() throws SQLException, NamingException {
      if (conn != null) return;
      Context ctx = new InitialContext();
      DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
      conn = ds.getConnection();   
   }
   
   public Result getAll(sName) throws SQLException, NamingException {
      try {
         open();
         Statement stmt = conn.createStatement();        
         ResultSet result = stmt.executeQuery("SELECT * FROM Customers where Name = '" + sName + "'");
         return ResultSupport.toResult(result);
      } finally {
         close();
      }
   }
   
   public void close() throws SQLException {
      if (conn == null) return;
      conn.close();
      conn = null;
   }  
}
Thanks,
Dave
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 20 2007
Added on Nov 21 2007
5 comments
859 views