|
Helping ordinary people create extraordinary websites! |
JSF for Nonbelievers: The JSF Application LifecycleBy Rick Hightower2005-05-05
Immediate Event Handling The final topic I'll cover is immediate event handling. Immediate event handling is useful in cases where you do not want (or need) to validate an entire page to process a user input. As you'll recall, the example application's cdForm.jsp page uses radio buttons to display a list of categories and subcategories. When a category is selected by the end user, the cdForm.jsp page uses JavaScript to post the form back so you can display a list of subcategories. This is an example of immediate event handling, since the whole form is not validated before the event handler is invoked. Instead, the event handler for the category list populates the subcategories and forces JSF to skip to the render response phase. The event handler for components normally executes in the invoke application phase. Event handlers for immediate event components execute in the apply request values phase, which happens before the process validation phase of the rest of the components. Listing 19 shows the category list on the cdForm.jsp page again. Listing 19. Category list on cdForm.jsp [cdForm.jsp]The selectOneRadio category field is bound to the CD's category property (value="#{CDManagerBean.cd.category}"). Note that immediate event handling is turned on (immediate="true"). This setting means that the Category component's events will be handled (and converted and validated) in the apply request values phase, rather than the invoke application phase. The JavaScript magic is in the line onclick="submit()" -- says that when the user makes a change, it should be immediately submitted to the Web application for processing. The event handler method The available categories that show up in the listing are determined by the f:selectItems tag's value (value="#{CDManagerBean.categories}"). The event handler for this component changing is the controller's categorySelected() method (valueChangeListener="#{CDManagerBean.categorySelected}"). The event handler is shown in Listing 20. Listing 20. categorySelected event handler [StoreController.java]The first thing the categorySelected() method does is allow the subCategoryList to render itself. The categorySelected() method then uses the selected category value to look up a list of subCategories. The subCategories property is bound to the values of the subcategoryList. Next, the event handler forces JSF to go to the render response phase by calling the renderResponse() method on the current FacesContext. Thus, the GUI (cdForm.jsp) will redisplay with the available subcategories for the current category displaying. Binding a component to the controller The subCategoryList component is bound from the GUI. Just like you can bind values to components, you can bind components into a controller. The subcategory is defined in the cdForm.jsp page, as shown in Listing 21. Listing 21. Subcategory list defined in the cdForm.jsp page [cdForm.jsp]The binding attribute allows you to bind components from the GUI into the backing bean (controller). Thus, this above component is bound to CDManagerBean.subCategoryList, which is a property in your controller defined as shown in Listing 22. Listing 22. subCategoryList property [StoreController.java ]Immediate event handling uses just a little bit of JavaScript magic (the onclick="submit()" command) and the convenience of the flexible JSF lifecycle to let you process input from a single component, rather than waiting for the entire page to be validated. In this example, I've shown how immediate processing of a Category component enables you to show subcategories without reloading the page. Tutorial Pages: » Walk Through the 6 Phases of JSF's Request Processing Lifecycle » The JSF Lifecycle: an Overview » Phase 1: Restore View » Phase 2: Apply Request Values » Phase 3: Process Validation » Phase 4: Update Model Values » Phase 5: Invoke application » Phase 6: Render Response » A Working Example » Let's Code it » Use Case 1: Add a New CD » Use Case 2: Edit a CD » Use Case 3: Sort CDs » Immediate Event Handling » Conclusion » Resources First published by IBM DeveloperWorks |
|