Core JavaServer Faces 3rd ed Edition David Geary download
Core JavaServer Faces 3rd ed Edition David Geary download
pdf download
https://ebookname.com/product/core-javaserver-faces-3rd-ed-
edition-david-geary/
https://ebookname.com/product/the-faces-of-terrorism-david-v-
canter/
https://ebookname.com/product/microwave-engineering-3rd-ed-
edition-david-m-pozar/
https://ebookname.com/product/the-child-surveillance-
handbook-3rd-ed-edition-david-elliman/
https://ebookname.com/product/paradoxes-of-modernization-
unintended-consequences-of-public-policy-reform-1st-edition-
helen-margetts/
The Structure of Philosophical Discourse A Genre and
Move Analysis 1st Edition Kyle Lucas
https://ebookname.com/product/the-structure-of-philosophical-
discourse-a-genre-and-move-analysis-1st-edition-kyle-lucas/
https://ebookname.com/product/encyclopedia-of-substance-
abuse-2-volume-set/
https://ebookname.com/product/willie-morris-an-exhaustive-
annotated-bibliography-and-a-biography-1st-edition-jack-bales/
https://ebookname.com/product/human-heredity-principles-and-
issues-9th-edition-michael-r-cummings/
https://ebookname.com/product/the-unofficial-guide-to-flipping-
properties-unofficial-guides-1st-edition-peter-a-richmond/
Encyclopedia of CAMPS AND GHETTOS 1933 1945 Vol 1 Part
B Early Camps Youth Camps and Concentration Camps and
Subcamps under the SS Business Administration Main
Office WVHA 1st Edition Geoffrey P. Megargee
https://ebookname.com/product/encyclopedia-of-camps-and-
ghettos-1933-1945-vol-1-part-b-early-camps-youth-camps-and-
concentration-camps-and-subcamps-under-the-ss-business-
administration-main-office-wvha-1st-edition-geoffrey-p-mega/
FACELETS PAGE LAYOUT RADIO BUTTONS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" page.xhtml
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <h:selectOneRadio value="#{bean1.condiment}>
<html xmlns="http://www.w3.org/1999/xhtml" <f:selectItems value="#{bean1.choices}" var="it"
xmlns:f="http://java.sun.com/jsf/core" itemLabel="#{it.description}"
xmlns:h="http://java.sun.com/jsf/html" itemValue="#{it.productId}"/>
xmlns:ui="http://java.sun.com/jsf/facelets"> </h:selectOneRadio>
<h:head>...</h:head>
<h:body>
<h:form> WEB-INF/classes/com/corejsf/SampleBean.java
... public class SampleBean {
</h:form> public Collection<Condiment> getChoices() { ...}
</h:body> public int getCondiment() { ... }
</html> public void setCondiment(int value) { ... }
...
TEXT FIELD }
WEB-INF/classes/com/corejsf/Condiment.java
page.xhtml public class Condiment {
public String getDescription() { ... }
<h:inputText value="#{bean1.luckyNumber}"> public int getProductId() { ... }
}
WEB-INF/classes/com/corejsf/SampleBean.java
@Named("bean1") // or @ManagedBean(name="bean1")
CONVERSION
@SessionScoped <h:outputText value="#{bean1.amount}">
public class SampleBean { <f:convertNumber type="currency"/>
public int getLuckyNumber() { ... } </h:outputText>
public void setLuckyNumber(int value) { ... }
... The number is displayed with currency symbol and
} group separator: $1,000.00
BUTTON VALIDATION
Using the bean validation framework (JSR 303)
public class SampleBean {
page.xhtml @Max(1000) private BigDecimal amount;
}
<h:commandButton value="press me" action="#{bean1.login}"/>
Page-level validation and conversion
WEB-INF/classes/com/corejsf/SampleBean.java
<h:inputText value="#{bean1.amount}" required="true">
public class SampleBean {
<f:validateDoubleRange maximum="1000"/>
public String login() {
</h:inputText>
if (...) return "success"; else return "error";
}
... Error messages
}
The outcomes success and error can be mapped to pages
in faces-config.xml. If no mapping is specified, the page Amount
/success.xhtml or /error.xhtml is displayed. <h:inputText id="amt" label="Amount" value="#{bean1.amount}"/>
<h:message for="amt"/>
GET REQUESTS
<f:metadata> RESOURCES
<f:viewParam name="item" value="#{bean1.currentItem}"/>
<f:viewParam name="userId" value="#{bean1.user}"/> page.xhtml
</f:metadata> <h:outputStylesheet library="css" name="styles.css"/>
Request parameters set bean properties before the ...
<h:message for="amt" errorClass="errors">
page is rendered.
<h:button value="Continue" outcome="#{bean1.continueOutcome}" resources/css/styles.css
includeViewParams="true"/>
.errors {
The getContinueOutcome method is called when the button is font-style: italic;
rendered. The view parameters are added to the color: red;
request URL. }
THIRD EDITION
THIRD EDITION
DAVID GEARY
CAY HORSTMANN
Preface xv
Acknowledgments xix
1 GETTING STARTED 2
Why JavaServer Faces? 3
A Simple Example 4
Ingredients 7
Directory Structure 8
Building a JSF Application 9
Deploying a JSF Application 11
Development Environments for JSF 13
An Analysis of the Sample Application 15
Beans 16
JSF Pages 17
Servlet Configuration 19
A First Glimpse of Ajax 21
JSF Framework Services 24
Behind the Scenes 26
Rendering Pages 27
Decoding Requests 28
The Life Cycle 29
Conclusion 31
2 MANAGED BEANS 32
Definition of a Bean 33
Bean Properties 36
Value Expressions 37
Backing Beans 38
CDI Beans 39
Message Bundles 40
Messages with Variable Parts 42
Setting the Application Locale 43
A Sample Application 45
Bean Scopes 51
Session Scope 52
Request Scope 53
Application Scope 54
Conversation Scope 54
View Scope 55
Custom Scopes 56
Configuring Beans 56
Injecting CDI Beans 56
Injecting Managed Beans 57
Bean Life Cycle Annotations 58
Configuring Managed Beans with XML 58
The Expression Language Syntax 63
Lvalue and Rvalue Modes 63
Using Brackets 64
Map and List Expressions 65
Calling Methods and Functions 66
Resolving the Initial Term 67
Composite Expressions 69
Method Expressions 70
Method Expression Parameters 71
Conclusion 71
3 NAVIGATION 72
Static Navigation 73
Dynamic Navigation 74
Mapping Outcomes to View IDs 75
The JavaQuiz Application 77
Redirection 86
Redirection and the Flash 87
RESTful Navigation and Bookmarkable URLs 88
View Parameters 89
GET Request Links 90
Specifying Request Parameters 91
Adding Bookmarkable Links to the Quiz Application 92
Advanced Navigation Rules 96
Wildcards 97
Using from-action 98
Conditional Navigation Cases 99
Dynamic Target View IDs 99
Conclusion 99
5 FACELETS 178
Facelets Tags 179
Templating with Facelets 181
Building Pages from Common Templates 183
Organizing Your Views 187
Decorators 193
Parameters 195
Custom Tags 195
Components and Fragments 198
Loose Ends 198
<ui:debug> 198
<ui:remove> 200
Handling Whitespace 202
Conclusion 202
Styles 215
Styles by Column 215
Styles by Row 216
The ui:repeat Tag 217
JSF Components in Tables 218
Editing Tables 222
Editing Table Cells 222
Deleting Rows 225
Database Tables 228
Table Models 232
Rendering Row Numbers 233
Finding the Selected Row 234
Sorting and Filtering 234
Scrolling Techniques 242
Scrolling with a Scrollbar 242
Scrolling with Pager Widgets 243
Conclusion 244
10 AJAX 384
Ajax and JSF 386
The JSF Life Cycle and Ajax 387
The JSF Ajax Recipe 388
The f:ajax Tag 389
Ajax Groups 392
Ajax Field Validation 394
Ajax Request Monitoring 396
JavaScript Namespaces 398
Handling Ajax Errors 400
Ajax Responses 400
The JSF 2.0 JavaScript Library 403
Passing Additional Ajax Request Parameters 405
Queueing Events 407
Coalescing Events 408
13 HOW DO I . . . ? 546
How do I find more components? 547
How do I support file uploads? 548
How do I show an image map? 557
How do I produce binary data in a JSF page? 559
How do I show a large data set, one page at a time? 568
How do I generate a pop-up window? 573
How do I selectively show and hide parts of a page? 581
How do I customize error pages? 582
How do I write my own client-side validation tag? 588
How do I configure my application? 595
How do I extend the JSF expression language? 596
How do I add a function to the JSF expression
language? 599
Index 609
When we heard about JavaServer Faces (JSF) at the 2002 JavaOne conference,
we were very excited. Both of us had extensive experience with client-side Java
programming—David in Graphic Java™, and Cay in Core Java™, both published
by Sun Microsystems Press—and we found web programming with servlets
and JavaServer Pages (JSP) to be rather unintuitive and tedious. JSF promised
to put a friendly face in front of a web application, allowing programmers to
think about text fields and menus instead of dealing with page flips and
request parameters. Each of us proposed a book project to our publisher, who
promptly suggested that we should jointly write the Sun Microsystems Press
book on JSF.
In 2004, the JSF Expert Group (of which David is a member) released the JSF 1.0
specification and reference implementation. A bug fix 1.1 release emerged
shortly afterward, and an incremental 1.2 release added a number of cleanups
and convenience features in 2006.
The original JSF specification was far from ideal. It was excessively general,
providing for use cases that turned out to be uninteresting in practice. Not
enough attention was given to API design, forcing programmers to write com-
plex and tedious code. Support for GET requests was clumsy. Error handling
was plainly unsatisfactory, and developers cursed the “stack trace from hell”.
JSF had one saving grace, however. It was highly extensible, and therefore it
was very attractive to framework developers. Those framework developers
xv
built cutting edge open-source software that plugged into JSF, such as Facelets,
Ajax4jsf, Seam, JSF Templates, Pretty Faces, RichFaces, ICEFaces, and so on.
JSF 2.0, released in 2009, is built on the experience of those open-source frame-
works. Nearly all of the original authors of the aforementioned frameworks
participated on the JSF 2 Expert Group, so JSF 2.0, unlike JSF 1.0, was forged
from the crucible of real-world open-source projects that had time to mature.
JSF 2.0 is much simpler to use and better integrated into the Java EE technology
stack than JSF 1.0. Almost every inch of JSF 1.0 has been transformed in JSF 2.0
in some way for the better. In addition, the specification now supports new
web technologies such as Ajax and REST.
JSF is now the preeminent server-side Java web framework, and it has fulfilled
most of its promises. You really can design web user interfaces by putting com-
ponents on a form and linking them to Java objects, without having to mix
code and markup. A strong point of JSF is its extensible component model, and
a large number of third-party components have become available. The flexible
design of the framework has allowed it to grow well and accommodate new
technologies.
Because JSF is a specification and not a product, you are not at the mercy of a
single vendor. JSF implementations, components, and tools are available from
multiple sources. We are very excited about JSF 2.0, and we hope you will share
in this excitement when you learn how this technology makes you a more
effective web application developer.
Required Software
All software that you need for this book is freely available. You can use an
application server that supports Java EE 6 (such as GlassFish version 3) or a
servlet runner (such as Tomcat 6) together with a JSF implementation. The
software runs on Linux, Mac OS X, Solaris, and Windows. Both Eclipse and
NetBeans have extensive support for JSF development with GlassFish or
Tomcat.
Web Support
The web site for this book is http://corejsf.com. It contains:
• The source code for all examples in this book
• Useful reference material that we felt is more effective in browseable form
than in print
• A list of known errors in the book and the code
• A form for submitting corrections and suggestions
First and foremost, we'd like to thank Greg Doench, our editor at Prentice Hall,
who has shepherded us through this project, never losing his nerve in spite of
numerous delays and complications. Many thanks to Vanessa Moore for turn-
ing our messy manuscript into an attractive book and for her patience and
amazing attention to detail.
We very much appreciate our reviewers for this and previous editions who
have done a splendid job, finding errors and suggesting improvements in
various drafts of the manuscript. They are:
• Gail Anderson, Anderson Software Group, Inc.
• Larry Brown, LMBrown.com, Inc.
• Damodar Chetty, Software Engineering Solutions, Inc.
• Frank Cohen, PushToTest
• Brian Goetz, Sun Microsystems, Inc.
• Rob Gordon, Crooked Furrow Farm
• Marty Hall, author of Core Servlets and JavaServer Pages™, Second Edition,
(Prentice Hall, 2008)
• Steven Haines, CEO/Founder, GeekCap, Inc.
• Charlie Hunt, Sun Microsystems, Inc.
• Jeff Langr, Langr Software Solutions
• Jason Lee, Senior Java Developer, Sun Microsystems, Inc.
xix
THIRD EDITION
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com