Chapter 5. Data Tables

Panels

Throughout this chapter we have used HTML tables to align form prompts and input fields. Creating table markup by hand is tedious and error prone, so now we'll look at alleviating some of that tediousness with h:panelGrid, which generates an HTML table element. You can specify the number of columns in the table with the columns attribute, like this:

  <h:panelGrid columns="3">       ...   </h:panelGrid>

The columns attribute is not mandatory if you do not specify it, the number of columns defaults to 1. h:panelGrid places components in columns from left to right and top to bottom. For example, if you have a panel grid with three columns and nine components, you will wind up with three rows, each containing three columns. If you specify three columns and ten components, you will have four rows, and in the last row only the first column will contain a component the tenth component.

Table 4-23 lists h:panelGrid attributes.

Table 4-23. Attributes for h:panelGrid
Attributes Description
bgcolor Background color for the table
border Width of the table's border
cellpadding Padding around table cells
cellspacing Spacing between table cells
columnClasses Comma-separated list of CSS classes for columns
columns Number of columns in the table
footerClass CSS class for the table footer
frame Specification for sides of the frame surrounding the table that are to be drawn; valid values: none, above, below, hsides, vsides, lhs, rhs, box, border
headerClass CSS class for the table header
rowClasses Comma-separated list of CSS classes for rows
rules Specification for lines drawn between cells; valid values: groups, rows, columns, all
summary Summary of the table's purpose and structure used for nonvisual feedback such as speech
captionClass (JSF 1.2), captionStyle (JSF 1.2) CSS class or style for the caption; a panel caption is optionally supplied by a facet named "caption"
binding, id, rendered, styleClass, value Basic attributes[a]
dir, lang, style, title, width HTML 4.0[b]
onclick, ondblclick, onkeydown, onkeypress, onkeyup, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup DHTML events[c]


[a] See Table 4-4 on page 97 for information about basic attributes.

[b] See Table 4-5 on page 101 for information about HTML 4.0 attributes.

[c] See Table 4-6 on page 102 for information about DHTML event attributes.

You can specify CSS classes for different parts of the table: header, footer, rows, and columns. The columnClasses and rowClasses specify lists of CSS classes that are applied to columns and rows, respectively. If those lists contain fewer class names than rows or columns, the CSS classes are reused. That makes it possible to specify classes, like this: rowClasses="evenRows, oddRows" and columnClasses="evenColumns, oddColumns".

The cellpadding, cellspacing, frame, rules, and summary attributes are HTML pass-through attributes that apply only to tables. See the HTML 4.0 specification for more information.

h:panelGrid is often used with h:panelGroup, which groups two or more components so they are treated as one. For example, you might group an input field and its error message, like this:

   <h:panelGrid columns="2">       ...       <h:panelGroup>          <h:inputText  value="#{user.name}">          <h:message for="name"/>       </h:panelGroup>       ...    </h:panelGrid>

Grouping the text field and error message puts them in the same table cell. Without that grouping, the error message component would occupy its own cell. In the absence of an error message, the error message component produces no output but still takes up a cell, so you wind up with an empty cell.

h:panelGroup is a simple tag with only a handful of attributes. Those attributes are listed in Table 4-24.

Table 4-24. Attributes for h:panelGroup
Attributes Description
layout (JSF 1.2) If the value is "block", use an HTML div to lay out the children. Otherwise, use a span.
binding, id, rendered, styleClass Basic attributes.[a]
style HTML 4.0.[b]


[a] See Table 4-4 on page 97 for information about basic attributes.

[b] See Table 4-5 on page 101 for information about HTML 4.0 attributes.

Figure 4-11 shows a simple example that uses h:panelGrid and h:panelGroup. The application contains a form that asks for the user's name and age. We have added a required validator with h:inputText's required attribute to the name field and used an h:message tag to display the corresponding error when that constraint is violated. We have placed no restrictions on the Age text field.

Figure 4-11. Using h:panelGrid and h:panelGroup


Notice that those constraints require three columns in the first row one each for the name prompt, text field, and error message but only two in the second: the age prompt and text field. Since h:panelGrid allows only one value for its columns attribute, we can resolve this column quandary by placing the name text field and error message in a panel group, and because those two components will be treated as one, we actually have only two columns in each row.

The directory structure for the application shown in Figure 4-11 is shown in Figure 4-12. Listings 4-20 through 4-22 contain the code of the JSF page and related files.

Figure 4-12. Directory structure for the panels example


Listing 4-20. panels/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.windowTitle}"/>   9.          </title>  10.       </head>  11.  12.       <body>  13.          <h:form>  14.             <h:panelGrid columns="2" rowClasses="oddRows,evenRows">  15.                <h:outputText value="#{msgs.namePrompt}:"/>  16.                <h:panelGroup>  17.                   <h:inputText   18.                      value="#{user.name}" required="true"  19.                      label="#{msgs.namePrompt}"/>  20.                   <h:message for="name" error/>  21.                </h:panelGroup>  22.                <h:outputText value="#{msgs.agePrompt}:"/>  23.                <h:inputText size="3" value="#{user.age}"  24.                   label="#{msgs.agePrompt}"/>  25.             </h:panelGrid>  26.             <br/>  27.             <h:commandButton value="#{msgs.submitPrompt}"/>  28.          </h:form>  29.       </body>  30.    </f:view>  31. </html>     

Listing 4-21. panels/src/java/com/corejsf/messages.properties

  1. windowTitle=Using h:panelGrid and h:panelGroup   2. namePrompt=Name   3. agePrompt=Age   4. submitPrompt=Submit form

Listing 4-22. panels/web/styles.css

  1. body {   2.    background: #eee;   3. }   4. .errors {   5.    font-style: italic;   6. }   7. .evenRows {   8.    background: PowderBlue;   9. }  10. .oddRows {  11.    background: MediumTurquoise;  12. }



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