JSF, CSS, and tables

There are two kinds of grids (tables) that are very often used in JSF, h:panelGrid and h:dataTable. Both of them can be styled with CSS in detail using a set of dedicated attributes. In this recipe you will see these attributes at work for h:panelGrid, but it is very simple to extrapolate this to h:dataTable.

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

Suppose that we have an h:panelGrid. We can "populate" it with CSS styles, using the following set of attributes:

Name

Description

columnClasses

This is used to specify the comma-separated list of CSS style classes to be applied on the columns of the table.

headerClass

This is used to specify the spaces-separated list of CSS style classes to be applied on the header of the table.

footerClass

This is used to specify the spaces-separated list of CSS style classes to be applied on the footer of the table.

rowClasses

This is used to specify the comma-separated list of CSS style classes to be applied on the rows of the table.

styleClass

This is used to set the CSS class for the component.

style

This is used to set the CSS style definition for the component.

Knowing these attributes, we build a JSF page to show you how to use them in practice (notice where we have placed the attributes):

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF and CSS example</title>
<style type="text/css">
.message { text-align: left;
letter-spacing:5px;
color:#000099
}
.message-font { font-family:georgia,garamond,serif;
font-size:20px;
font-style:italic;
}
.odd { background-color: blue }
.even { background-color: red }
</style>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="3" border="1" footerClass="message"
headerClass="message-font" rowClasses="odd, even"
title="PanelGrid and CSS">
<f:facet name="header">
<h:outputText value="Fill Names Below"/>
</f:facet>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<h:inputText/>
<f:facet name="footer">
<h:outputText value="The End"/>
</f:facet>
</h:panelGrid>
</h:form>
</h:body>
</html>

How it works...

Since we have an attribute for each part of a grid, we can easily specify CSS styles to customize the design of each of these parts. JSF will combine the specified CSS styles to render a cool grid to the user.

There's more...

The h:dataTable allows you to use the same CSS attributes for table header, footer, and so on.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: JSF_CSS_and_tables

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset