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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 ■ JAVASERVER FACESRenderers are organized into render kits, which focus on a specific type of output. To ensureapplication portability, JSF includes support for a standard render kit and associated renderers forHTML 4.01. JSF implementations can then create their own render kit to generate Wireless MarkupLanguage (WML), Scalable Vector Graphics (SVGs), and so on.Converters and ValidatorsOnce the page is rendered, the user can interact <strong>with</strong> it to enter data. As there are no type constraints, arenderer cannot know beforehand how to display the object. This is where converters come in: theytranslate an object (Integer, Date, Enum, Boolean, etc.) to a string for display, and from an input stringback into an object. JSF comes <strong>with</strong> a set of converters for common types (in the javax.faces.convertpackage), but you can develop your own or incorporate third-party types.Sometimes this data also has to be validated before being processed in the back end. Validators areresponsible for ensuring that the value entered by a user is acceptable. One or more validators can beassociated <strong>with</strong> a single component. JSF comes <strong>with</strong> a few validators (LengthValidator, RegexValidator,etc.) and allows you to create your own using annotated classes. When there’s a conversion or validationerror, a message is sent back to the response to be displayed.Managed Beans and NavigationAll of the concepts so far have been related to a single page: what is a page, what is a component, howare they rendered, converted, and validated. Web applications are made up of multiple pages and needto perform business logic (by calling an EJB layer, for example). Going from one page to another,invoking EJBs, and synchronizing data <strong>with</strong> components is handled by managed beans.A managed bean is a specialized <strong>Java</strong> class that synchronizes values <strong>with</strong> components, processesbusiness logic, and handles navigation between pages. You associate a component <strong>with</strong> a specificmanaged bean property or action using the Expression Language (EL). Using some snippets of codefrom the previous example:The first line of code hooks up the input text’s value directly to the book.isbn property of a managedbean called bookController. The value of the input text is synchronized <strong>with</strong> the book.isbn property ofthe managed bean.A managed bean also handles events. The second line of code shows a submit button associated<strong>with</strong> an action. When the submit button is clicked, it triggers an event to the managed bean where anevent listener method is executed (here the doCreateBook() method).Listing 10-2 shows the BookController managed bean. This <strong>Java</strong> class is annotated <strong>with</strong>@ManagedBean, and it has a property, book, that is synchronized <strong>with</strong> the component’s value of the page.The doCreateBook() method invokes a stateless EJB and then returns a string that allows navigationbetween pages.281

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

Saved successfully!

Ooh no, something went wrong!