|
Helping ordinary people create extraordinary websites! |
JSF for Nonbelievers: The JSF Application LifecycleBy Rick Hightower2005-05-05
Use Case 3: Sort CDs The final use case I'll walk you through will show how to sort tables. This use case also starts at the CD listing page. The listing page allows CDs to be sorted by title and artist in ascending and descending order. In this example, I'll show you how to sort by title, leaving sorting by artist as a learning exercise. The header for title sorts has links to sort methods on the controller. Listing 16 shows how the title's header is listed in listing.jsp. Listing 16. Sorting commandLinks [listing.jsp]The panelGroup component Notice in Listing 16 that the links are defined inside of the header facet for the title column. A facet only associates a single-named component; therefore, to place multiple-link components inside of the header facet, you need to use a panelGroup. A panelGroup (similar to a panelGrid) is a single component that contains a number of child components. The panelGroup contains two links, as shown in Listing 17. Listing 17. panelGroup component links [listing.jsp]The first link is bound to the controller method sortTitleAsc, and the second is bound to sortTitleDec. These methods are listed as shown in Listing 18. Listing 18. panelGroup link methods [StoreController.java ]Both of these methods rely on the business delegate to return a java.util.List sorted in the correct manner. Notice that the methods return the logical outcomes asc and dec. Neither of these outcomes has a mapping in the faces-config.xml file. Outcomes that do not have mappings cause the current view to be reloaded; thus, the listing.jsp would be reloaded when these methods were called, and the list would be redisplayed in the correct order. The beauty of this approach is that it relies on the business delegate to do the sorting. The business delegate may in turn rely on a DAO object that in turn relies on a database query or an OR mapping query that sorts the CDs very efficiently. This is usually a much better approach than having "smart" GUI components that know how to sort random domain objects (CD is a domain object) because the sorting is often, strictly speaking, part of the model (that is, part of the domain object), not the view. As mentioned, the code for sorting by title and sorting by artist is nearly the same. As a learning exercise, try writing the code for the fourth use case yourself, but this time, sort by artist rather than by title. 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 |
|