This chapter has been leading us gently through the JavaServer Faces technology. More importantly, in it, Budi teaches ushow to write an online survey application to get a feel for how this great technology works. This chapter prepares you for the next chapters by introducing the JSF Application Programming Interface (API) and the Application Configuration file. This excerpt comes from chapter two of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).
This section presents another sample application, which features an online survey application with two JSP pages. What makes it different from the previous two examples is that this application has multiple pages. Its main purpose is to demonstrate how to navigate from one JSP page to another.
Like the first two applications, this application is simple. The first page displays a form that takes two input boxes, where you enter your favorite singer and favorite band. After you submit the form, the second JSP page is displayed. The second JSP page displays the values you entered into the form in the first page. It also allows you to go back to the first page by clicking a button. A JavaBean called SurveyBean is used to store the user’s data.
This application requires a directory structure, deployment descriptor (the same as the ones for the previous examples, shown in Listing 1), a JavaBean, an application configuration file, and two JSP pages.
Creating the Directory Structure for the Page Navigation Example
You need to create a directory called JSPCh02c for this application. In Tomcat, you create the directory under the webapps directory. The directory structure for this application is shown in Figure 10. Notice that it has two JSP pages: page1.jsp and page2.jsp. Also, the SurveyBean class resides under the WEB-INF/classes directory. As usual, you must copy all required libraries to the WEB-INF/lib directory.
Creating the SurveyBean Your application needs a JavaBean to store data. This bean is called SurveyBean, and it can be found in the SurveyBean.java file in the ch02c package. It is shown in Listing 7.
Figure 10The directory structure of the page navigation example
Listing 7The SurveyBean
package ch02c
; public class SurveyBean { String favSinger = null; String favBand = null; public void setFavSinger(String favSinger) { this.favSinger = favSinger; } public String getFavSinger() { return favSinger; } public void setFavBand(String favBand) { this.favBand = favBand; } public String getFavBand() { return favBand; <?xml:namespace prefix = f /><f:use_faces>} }
There is nothing special in the SurveyBean class. It has two properties, favSinger and favBand, and accessors and mutators (get and set methods) for the properties.
Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you've enjoyed what you've seen here, click on the "Buy it now!" banner to pick up a copy today! Buy this book!