12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ JAVASERVER FACES• Converters: These convert a component’s value (Date, Boolean, etc.) to and frommarkup values (String).• Validators: These are responsible for ensuring that the value entered by a user isvalid.• Managed bean and navigation: The business logic is made in managed beans,which also control the navigation between pages.• Ajax support: JSF 2.0 comes <strong>with</strong> built-in support for Ajax as explained inChapter 12.FacesServlet and faces-config.xmlMost of the web frameworks use the Model-View-Controller (MVC) design pattern, as does JSF. TheMVC pattern is used to decouple the view (the page) and the model (the data to be displayed in theview). The controller handles user actions that might result in changes in the model and updates to theviews. In JSF, this controller is a servlet called FacesServlet. All user requests go through theFacesServlet, which examines the request and calls various actions on the model using managed beans.This servlet is internal and part of JSF. The only way it can be configured is by using externalmetadata. Up to JSF 1.2, the only source of configuration was the faces-config.xml file. Today, <strong>with</strong>JSF 2.0, this file is optional, and most metadata can be defined through annotations (on managed beans,converters, components, renderers, and validators).Pages and ComponentsThe JSF framework has to send a page to the client’s output device (a browser, for example) and requiressome sort of display technology. This display technology is the PDL (aka VDL). A JSF application is freeto use several technologies for its PDL such as JSP or Facelets. A JSF 2.0 implementation that claimscompliance <strong>with</strong> the specification must include a complete JSP implementation. JSP was the default PDLin JSF 1.1 and JSF 1.2, but Facelets is preferred for JSF 2.0.Both JSP and Facelets pages are made up of a tree of components (also called widgets or controls)that provide specific functionality for interacting <strong>with</strong> an end user (text field, button, list box, etc.). JSFhas a standard set of components and allows you to easily create your own. The page goes through acomplex life cycle to manage this tree of components (initialization, events, rendering, etc.).The code in Listing 10-1 is a Facelets XHTML page that uses JSF tags(xmlns:h="http://java.sun.com/jsf/html") to display a form <strong>with</strong> two input fields (the ISBN and thetitle of a book) and a button. This page is composed of several JSF components. Some of them have novisual appearance, like the ones used to declare the header (), the body (), or the form(). Others are visual and represent a label (), a text field (), or abutton (). Notice that pure HTML tags can also be mixed in the page (, ,, etc.).279

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!