Professional Documents
Culture Documents
JSF Cheat Sheet PDF
JSF Cheat Sheet PDF
#21
tech facts at your fingertips
Get More Refcardz! Visit refcardz.com
CONTENTS INCLUDE:
JavaServer Faces
n
Development Process
n
Lifecycle
n
Faces-config.xml
n
The JSF Expression Language
JSF Core Tags
By Cay S. Horstmann
n
n
JSF HTML Tags and more...
These common tasks give you a crash course into using JSF.
ABOUT THIS REFCARD
Text field
JavaServer Faces (JSF) is the “official” component-based
page.jspx
view technology in the Java EE web tier. JSF includes a set <h:inputText value="#{bean1.luckyNumber}">
of predefined UI components, an event-driven programming
model, and the ability to add third-party components. JSF faces-config.xml
<managed-bean>
is designed to be extensible, easy to use, and toolable. This
<managed-bean-name>bean1</managed-bean-name>
refcard describes the JSF development process, standard JSF <managed-bean-class>com.corejsf.SampleBean</
tags, the JSF expression language, and the faces-config.xml managed-bean-class>
configuration file. <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
uri="http://
<jsp:output omit-xml-declaration="no"
java.sun.com/jsf/
doctype-root-element="html"
html" doctype-public="-//W3C//DTD XHTML 1.0
prefix="h" %> Transitional//EN"
<f:view> doctype-system="http://www.w3.org/TR/
<head> xhtml1/
<title>...</ DTD/xhtml1-transitional.dtd"/>
title> <f:view>
</head> <html xmlns="http://www.w3.org/1999/
<body> xhtml">
<h:form> <head>
... <title>...</title>
</h:form> </head>
<body>
</body>
<h:form>...</h:form>
</f:view>
</body>
</html>
</html>
</f:view>
</jsp:root>
page.jspx page.jspx
<h:inputText value="#{payment.amount}" <h:dataTable value="#{bean1.entries}" var="row"
required="true"> styleClass="table" rowClasses="even,odd">
<f:validateDoubleRange maximum="1000"/> <h:column>
</h:inputText> <f:facet name="header">
<h:outputText value="#{payment.amount}"> <h:outputText value="Name"/>
<f:convertNumber type="currency"/> </f:facet>
<!-- number displayed with currency symbol and <h:outputText value="#{row.name}"/>
group separator: $1,000.00 --> </h:column>
</h:outputText> <h:column>
<h:commandLink value="Delete" action="#{bean1.
Error Messages deleteAction}" immediate="true">
<f:setPropertyActionListener target="#{bean1.
idToDelete}"
value="#{row.id}"/>
page.jspx </h:commandLink>
<h:outputText value="Amount"/> </h:column>
</h:dataTable>
<h:inputText id="amount" label="Amount"
value="#{payment.amount}"/> com/corejsf/SampleBean.java
<!-- label is used in message text -->
public class SampleBean {
<h:message for="amount"/>
private int idToDelete;
Resources and Styles public void setIdToDelete(int value) {idToDelete
= value; }
page.jspx public String deleteAction() {
<head> // delete the entry whose id is idToDelete
<link href="styles.css" rel="stylesheet" return null;
type="text/css"/> }
... public List<Entry> getEntries() {...}
</head> ...
<body> }
LIFECYCLE web.xml
m Optional initialization (not shown here) paramValues A Map of HTTP request parameters, yielding a String[] array of
all values for a given name.
n validator cookie A Map of the cookie names and values of the current request.
m validator-id
initParam A Map of the initialization parameters of this web application.
m validator-class
requestScope A Map of all request scope attributes.
m Optional initialization (not shown here)
sessionScope A Map of all session scope attributes.
n lifecycle
applicationScope A Map of all application scope attributes.
m phase-listener
facesContext The FacesContext instance of this request.
n component, factory, referenced-bean, render-kit,
view The UIViewRoot instance of this request.
faces-config-extension (details not shown)
The JSF Expression Language (EL), continued JSF Core Tags, continued
There are two expression types: Tag Description/ Attributes
n Value expression: a reference to a bean property or an f:convertNumber Adds a number converter to a component
type number (default), currency , or
entry in a map, list, or array. Examples: percent
userBean.name calls getName or setName on the userBean pattern Formatting pattern, as defined in
java.text.DecimalFormat
object
maxFractionDigits Maximum number of digits in the
pizza.choices[var] calls pizza.getChoices( ).get(var) fractional part
or pizza.getChoices( ).put(var, ...) minFractionDigits Minimum number of digits in the
fractional part
n Method expression: a reference to a method and the maxIntegerDigits Maximum number of digits in the
object on which it is to be invoked. Example: integer part
minIntegerDigits Minimum number of digits in the
u
serBean.login calls the login method on the userBean integer part
object when it is invoked. integerOnly True if only the integer part is
parsed (default: false)
In JSF, EL expressions are enclosed in #{...} to indicate groupingUsed True if grouping separators are
deferred evaluation. The expression is stored as a string and used (default: true)
locale Locale whose preferences are to
evaluated when needed. In contrast, JSP uses immediate be used for parsing and formatting
evaluation, indicated by ${...} delimiters. currencyCode ISO 4217 currency code to use
when converting currency values
currencySymbol Currency symbol to use when
converting currency values
JSF CORE TAGS
f:validator Adds a validator to a component
validatorId The ID of the validator
Tag Description/ Attributes
f:validateDoubleRange Validates a double or long value, or the length of a string
f:view Creates the top-level view
f:validateLongRange minimum, maximum the minimum and maximum of the
locale The locale for this view. f:validateLength valid rang
renderKitId The render kit ID for this view
(JSF 1.2) f:loadBundle Loads a resource bundle, stores properties as a Map
basename The resource bundle name
beforePhase, Phase listeners that are called in
afterPhase every phase except "restore view" value The name of the variable that is bound to
the bundle map
f:subview Creates a subview of a view
f:selectitems Specifies items for a select one or select many component
binding, id, rendered Basic attributes
binding, id Basic attributes
f:facet Adds a facet to a component value Value expression that points to a
SelectItem, an array or Collection of
name the name of this facet SelectItem objects, or a Map mapping
labels to values.
f:attribute Adds an attribute to a component
name, value the name and value of the attribute to set f:selectitem Specifies an item for a select one or select many
component
f:param Constructs a parameter child component binding, id Basic attributes
name An optional name for this parameter itemDescription Description used by tools only
component.
itemDisabled Boolean value that sets the item’s
value The value stored in this component. disabled property
binding, id Basic attributes itemLabel Text shown by the item
itemValue Item’s value, which is passed to the
f:actionListener Adds an action listener or value change listener to a server as a request parameter
f:valueChangeListener component
value Value expression that points to a
type The name of the listener class SelectItem instance
f:setPropertyChange Adds an action listener to a component that sets a bean f:verbatim Adds markup to a JSF page
Listener (JSF 1.2) property to a given value. escape If set to true, escapes <, >, and &
characters. Default value is false.
value The bean property to set when the
action event occurs rendered (JSF 1.2) Basic attributes
dateStyle default, short, medium, long, or full h:inputText Single-line text input control
h:messages Displays all messages accesskey, alt, dir, HTML 4.0 pass-through attributes—alt, maxlength,
disabled, lang, maxlength, and size do not apply to h:inputTextarea. None
readonly, size, style, apply to h:inputHidden
tabindex, title
h:grapicImage Displays an image onblur, onchange, onclick, DHTML events. None apply to h:inputHidden
ondblclick, onfocus,
onkeydown, onkeypress,
h:selectOneListbox Single-select listbox onkeyup, onmousedown,
onmousemove, onmouseout,
onmouseover, onselect
h:selectOneRadio Set of radio buttons escape If set to true, escapes <, >, and &
characters. Default value is true.
h:selectBooleanCheckbox Checkbox binding, converter, id, rendered, Basic at tributes
styleClass, value
h:selectManyCheckbox Multiselect listbox
style, title HTML 4.0
h:selectManyListbox Multiselect listbox
Attribute Description alt, dir, height, ismap, lang, longdesc, style, HTML 4.0
title, url, usemap, width
id Identifier for a component
onblur, onchange, onclick, ondblclick, onfocus, DHTML events
binding Reference to the component that can be used in a backing
onkeydown, onkeypress, onkeyup, onmousedown,
bean
onmousemove, onmouseout, onmouseover,
rendered A boolean; false suppresses rendering onmouseup
styleClass Cascading stylesheet (CSS) class name
value A component’s value, typically a value binding Attributes for h:commandButton
valueChangeListener A method binding to a method that responds to value and h:commandLink
changes
Attribute Description
converter Converter class name
validator Class name of a validator that’s created and attached to a action If specified as a string: Directly specifies an
component outcome used by the navigation handler to
determine the JSF page to load next as a result
required A boolean; if true, requires a value to be entered in the of activating the button or link If specified as a
associated field method binding: The method has this signature:
String methodName(); the string represents
the outcome
Attributes for h:form actionListener A method binding that refers to a method with
this signature: void methodName(ActionEvent)
Attribute Description charset For h:commandLink only—The character
binding, id, rendered, styleClass Basic attributes encoding of the linked reference
image For h:commandButton only—A context-relative
accept, acceptcharset, dir, enctype, lang, HTML 4.0 attributes path to an image displayed in a button. If you
style, target, title (acceptcharset corresponds to specify this attribute, the HTML input’s type will
HTML accept-charset) be image.
onblur, onchange, onclick, ondblclick, onfocus, DHTML events immediate A boolean. If false (the default), actions and action
onkeydown, onkeypress, onkeyup, onmousedown,
listeners are invoked at the end of the request
onmousemove, onmouseout, onmouseover, onreset,
life cycle; if true, actions and action listeners are
onsubmit
invoked at the beginning of the life cycle.
first Index of the first row shown in the table onclick, ondblclick, DHTML events
onkeydown, onkeypress,
footerClass CSS class for the table footer onkeyup, onmousedown,
frame Frame Specification for sides of the frame surrounding the onmousemove, onmouseout,
table that are to be drawn; valid values: none, above, below, onmouseover, onmouseup
hsides, vsides, lhs, rhs, box, border
headerClass CSS class for the table header Attributes for h:column
rowClasses Comma-separated list of CSS classes for rows Attribute Description
rules Specification for lines drawn between cells; valid values: headerClass (JSF 1.2) CSS class for the column's header
groups, rows, columns, all
footerClass (JSF 1.2) CSS class for the column's footer
summary Summary of the table's purpose and structure used for non-
visual feedback such as speech binding, id, rendered Basic attributes
Cay S. Horstmann
Core JavaServer Faces
Cay S. Horstmann has written many books on C++, Java and object-
oriented development, is the series editor for Core Books at Prentice-Hall delves into all facets of
and a frequent speaker at computer industry conferences. For four years, JSF development, offering
Cay was VP and CTO of an Internet startup that went from 3 people in a systematic best practices for
tiny office to a public company. He is now a computer science professor building robust applications
at San Jose State University. He was elected Java Champion in 2005.
and maximizing developer
List of recent books/publications productivity.
• C ore Java, with Gary Cornell, Sun Microsystems Press 1996 - 2007 (8 editions)
• Core JavaServer Faces, with David Geary, Sun Microsystems Press, 2004-2006 (2 editions)
• Big Java, John Wiley & Sons 2001 - 2007 (3 editions)
BUY NOW
Blog Web site
books.dzone.com/books/jsf
http://weblogs.java.net/blog/cayhorstmann http://horstmann.com
DZone, Inc.
1251 NW Maynard
ISBN-13: 978-1-934238-19-6
Cary, NC 27513
ISBN-10: 1-934238-19-8
50795
888.678.0399
DZone communities deliver over 4 million pages each month to 919.678.0300
more than 1.7 million software developers, architects and decision
Refcardz Feedback Welcome
makers. DZone offers something for everyone, including news, refcardz@dzone.com
$7.95
tutorials, cheatsheets, blogs, feature articles, source code and more. Sponsorship Opportunities 9 781934 238196
“DZone is a developer’s dream,” says PC Magazine. sales@dzone.com
Copyright © 2008 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, Version 1.0
photocopying, or otherwise, without prior written permission of the publisher. Reference: Core JavaServer Faces, David Geary and Cay S. Horstmann, Sun Microsystems Press, 2004-2006.