Table Models

Database Tables

Databases store information in tables, so the JSF data table component is a good fit for showing data stored in a database. In this section, we show you how to display the results of a database query.

Figure 5-10 shows a JSF application that displays a database table.

Figure 5-10. Displaying database tables


The JSF page shown in Figure 5-10 uses h:dataTable, like this:

  <h:dataTable value="#{customer.all}" var="currentCustomer"      style      header      columnClasses="custid,name">         <h:column>         <f:facet name="header">            <h:outputText value="#{msgs.customerIdHeader}"/>         </f:facet>         <h:outputText value="#{currentCustomer.Cust_ID}"/>      </h:column>      <h:column>         <f:facet name="header">            <h:outputText value="#{msgs.nameHeader}"/>         </f:facet>            <h:outputText value="#{currentCustomer.Name}"/>      </h:column>      ...   </h:dataTable>

The customer bean is a managed bean that knows how to connect to a database and perform a query of all customers in the database. The CustomerBean.all method performs that query.

The preceding JSF page accesses column data by referencing column names for example, #{customer.Cust_ID} references the Cust_ID column.

The directory structure for the database example is shown in Figure 5-11. Listings for the application are given in Listing 5-13 through Listing 5-16.

Figure 5-11. Directory structure for the database example


Listing 5-13. database/web/index.jsp

  1. <html>   2.    <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>   3.    <%@ taglib uri="http://java.sun.com/jsf/html"  prefix="h" %>   4.    <f:view>   5.       <head>   6.          <link href="styles.css" rel="stylesheet" type="text/css"/>   7.          <title>   8.             <h:outputText value="#{msgs.pageTitle}"/>   9.          </title>  10.       </head>  11.       <body>  12.          <h:form>  13.             <h:dataTable value="#{customer.all}" var="customer"  14.                style  15.                header columnClasses="custid,name">  16.                <h:column>  17.                   <f:facet name="header">  18.                      <h:outputText value="#{msgs.customerIdHeader}"/>  19.                   </f:facet>  20.                   <h:outputText value="#{customer.Cust_ID}"/>  21.                </h:column>  22.                <h:column>  23.                   <f:facet name="header">  24.                      <h:outputText value="#{msgs.nameHeader}"/>  25.                   </f:facet>  26.                   <h:outputText value="#{customer.Name}"/>  27.                </h:column>  28.                <h:column>  29.                   <f:facet name="header">  30.                      <h:outputText value="#{msgs.phoneHeader}"/>  31.                   </f:facet>  32.                   <h:outputText value="#{customer.Phone_Number}"/>  33.                </h:column>  34.                <h:column>  35.                   <f:facet name="header">  36.                      <h:outputText value="#{msgs.addressHeader}"/>  37.                   </f:facet>  38.                   <h:outputText value="#{customer.Street_Address}"/>  39.                </h:column>  40.                <h:column>  41.                   <f:facet name="header">  42.                      <h:outputText value="#{msgs.cityHeader}"/>  43.                   </f:facet>  44.                   <h:outputText value="#{customer.City}"/>  45.                </h:column>  46.                <h:column>  47.                   <f:facet name="header">  48.                      <h:outputText value="#{msgs.stateHeader}"/>  49.                   </f:facet>  50.                   <h:outputText value="#{customer.State}"/>  51.                </h:column>  52.             </h:dataTable>  53.          </h:form>  54.       </body>  55.    </f:view>  56. </html>     

Listing 5-14. database/src/java/com/corejsf/CustomerBean.java

  1. package com.corejsf;   2.   3. import java.sql.Connection;   4. import java.sql.ResultSet;   5. import java.sql.SQLException;   6. import java.sql.Statement;   7. import javax.naming.Context;   8. import javax.naming.InitialContext;   9. import javax.naming.NamingException;  10. import javax.servlet.jsp.jstl.sql.Result;  11. import javax.servlet.jsp.jstl.sql.ResultSupport;  12. import javax.sql.DataSource;  13.  14. public class CustomerBean {  15.    private Connection conn;  16.  17.    public void open() throws SQLException, NamingException {  18.       if (conn != null) return;  19.       Context ctx = new InitialContext();  20.       DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");  21.       conn = ds.getConnection();  22.    }  23.  24.    public Result getAll() throws SQLException, NamingException {  25.       try {  26.          open();  27.          Statement stmt = conn.createStatement();  28.          ResultSet result = stmt.executeQuery("SELECT * FROM Customers");  29.          return ResultSupport.toResult(result);  30.       } finally {  31.          close();  32.       }  33.    }  34.  35.    public void close() throws SQLException {  36.       if (conn == null) return;  37.       conn.close();  38.       conn = null;  39.    }  40. }     

Listing 5-15. database/web/WEB-INF/web.xml

  1. <?xml version="1.0"?>   2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"   3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   5.       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   6.    version="2.5">   7.    <servlet>   8.       <servlet-name>Faces Servlet</servlet-name>   9.       <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  10.       <load-on-startup>1</load-on-startup>  11.    </servlet>  12.  13.    <servlet-mapping>  14.       <servlet-name>Faces Servlet</servlet-name>  15.       <url-pattern>*.faces</url-pattern>  16.    </servlet-mapping>  17.  18.    <welcome-file-list>  19.       <welcome-file>index.html</welcome-file>  20.    </welcome-file-list>  21.  22.    <resource-ref>  23.       <res-ref-name>jdbc/mydb</res-ref-name>  24.       <res-type>javax.sql.DataSource</res-type>  25.       <res-auth>Container</res-auth>  26.    </resource-ref>  27. </web-app>     

Listing 5-16. database/src/java/com/corejsf/messages.properties

  1. pageTitle=Displaying Database Tables   2. customerIdHeader=Customer ID   3. nameHeader=Name   4. phoneHeader=Phone Number   5. addressHeader=Address   6. cityHeader=City   7. stateHeader=State   8. refreshFromDB=Read from database

JSTL Result Versus Result Sets

The value you specify for h:dataTable can be, among other things, an instance of javax.servlet.jsp.jstl.Result or an instance of java.sql.ResultSet as was the case in "Database Tables" on page 191. h:dataTable wraps instances of those objects in instances of ResultDataModel and ResultSetDataModel, respectively. So how do the models differ? And which should you prefer?

If you have worked with result sets, you know they are fragile objects that require a good deal of programmatic control. The JSTL Result class is a bean that wraps a result set and implements that programmatic control for you; results are thus easier to deal with than are result sets.

On the other hand, wrapping a result set in a result involves some overhead in creating the Result object; your application may not be able to afford the performance penalty.

In the application discussed in "Database Tables" on page 191, we follow our own advice and return a JSTL Result object from the CustomerBean.all method.



Core JavaServerT Faces
Core JavaServer(TM) Faces (2nd Edition)
ISBN: 0131738860
EAN: 2147483647
Year: 2004
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net