The large scale design of a JSF application is a matter of determining the information content of its JSF pages (web screens) and the navigation between those pages. Presentation — choice of presentation widget types, layout, and styling — of the information is, of course, important. However, concern with presentation details before deciding on information content and navigation is likely to result in wasted effort. Developers and customers can make better decisions about them when they have a prototype in front of them — a prototype that shows the kinds of information that is presented and shows how the user navigates between the JSF pages.
A good general approach to JSF application design is to think of the application as an interconnected group of JSF pages, each group dealing with a particular task of the application. Each task needs a conversation scoped task bean to carry information between its JSF pages.
Real world applications can have many tasks and their interconnection can be quite complex. Dealing with more complex applications is in part a matter of developing its tasks in isolation. Then the remaining significant part of design is adding beans to glue the tasks together, providing navigation between them.
Dealing with these issues is not the objective of this presentation. Instead, it deals with a simple trivia quiz application that has a single task. The approach focuses first on navigation, then on information required in the controller bean for the task.
The trivia quiz web application lets the user play a trivia game with questions from several categories. As the game is played, results from the previous question and a running score are displayed. After the user finishes a quiz from one category she is given an opportunity to try another category.
Navigation from index.xhtml is initiated when the user clicks on one of the category command buttons. It always takes the user to first.xhtml.
Although the navigation is static, an action method serves two purposes:
Navigation from both of these JSF pages is initiated when the user clicks on the "Check Answer" command button. The navigation is dynamic, going to either next.xhtml or done.xhtml, depending on whether or not there are more problems in the category.
In addition to providing dynamic navigation, an action method is useful for changing bean state. In this case, you can update bean attributes such as the current problem number and score.
Sometimes this kind of updating can be built into bean property setters. That could be made to work for the trivia quiz.
Navigation from index.xhtml is initiated when the user clicks on the "Another quiz?" command button. It always takes the user to index.xhtml.
Although the navigation is static, an action method has an important purpose:
After getting a sense for navigation, you can determine what kind of information a managed bean needs to deal with. That information includes output data (bean property getters) and input data (bean property setters). It may also include data that is used only for navigation purposes.
Output properties are information that appears on JSF pages that is dependent on bean state. They should be implemented as bean properties with at least read (getter) access. Some output properties not immediately obvious. If you have a <h:inputText> tag, for instance, it needs an output property to specify the text displayed initially in the text field. The getter could just return an empty string if that is appropriate.
For the trivia quiz application, the index.xhtml page does not display any output properties. It is left as an exercise to determine the output properties needed by the other JSF pages.
Input properties are information that is entered by the user on JSF pages. They are used to set bean state so they should be implemented as bean properties with at least write (setter) access.
For the trivia quiz application, the index.xhtml page has an input property: the category name. However, that property is passed to the quiz bean as an action method parameter rather than through a bean property setter. It is left as an exercise to determine the input properties needed by the other JSF pages.