Online Store Application - Registering the Beans in the Application Configuration File
(Page 7 of 11 )
You must register the MenuBean, SearchBean, BrowseBean, ProductBean, ProductDetailsBean, and OrderBean in the application configuration file, so that they can be instantiated properly. Listing 14-13 shows the managed-bean tags in the application configuration file.
Listing 14-13 Managed-Bean Registration in the Application Configuration File
<managed-bean>
<managed-bean-name>MenuBean</managed-bean-name>
<managed-bean-class>buydirect.MenuBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>SearchBean</managed-bean-name>
<managed-bean-class>buydirect.SearchBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>BrowseBean</managed-bean-name>
<managed-bean-class>buydirect.BrowseBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>ProductBean</managed-bean-name>
<managed-bean-class>buydirect.ProductBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>ProductDetailsBean</managed-bean-name>
<managed-bean-class>
buydirect.ProductDetailsBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>imageDir</property-name>
<value>images/</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>ShoppingCartBean</managed-bean-name>
<managed-bean-class>
buydirect.ShoppingCartBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>OrderBean</managed-bean-name>
<managed-bean-class>buydirect.OrderBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
The MenuBean has the application context because the instance is used by the whole application. The SearchBean, BrowseBean, ProductBean,and ProductDetailsBean have the request scope because they are specific to a particular user request. Notice that the ProductDetailsBean has its imageDir property set by using the managed-property tag. The ShoppingCartBean and OrderBean have the session scope because they must live throughout a user’s session.
Establishing Navigation Rules You need navigation rules to manage the flow of your program. You use the JSP page names to define the navigation rules in the application file. Table 14-5 presents the JSP page name for each BuyDirect application page that needs navigation rules. (We will discuss these and the other JSP pages in the application in the “Creating the JSP Pages” section later in this chapter.)
Listing 14-14 shows the navigation-rule tags in the application configuration file.
Listing 14-14 Navigation Rules in the Application Configuration File
<navigation-rule>
<from-tree-id>*</from-tree-id>
<navigation-case>
<from-outcome>search</from-outcome>
<to-tree-id>/search.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/search.jsp</from-tree-id>
<navigation-case>
<from-outcome>detail</from-outcome>
<to-tree-id>/details.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/browse.jsp</from-tree-id>
<navigation-case>
<from-outcome>detail</from-outcome>
<to-tree-id>/details.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/details.jsp</from-tree-id>
<navigation-case>
<from-outcome>buy</from-outcome>
<to-tree-id>/shoppingCart.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/shoppingCart.jsp</from-tree-id>
<navigation-case>
<from-outcome>checkOut</from-outcome>
<to-tree-id>/checkOut.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/checkOut.jsp</from-tree-id>
<navigation-case>
<from-outcome>order</from-outcome>
<to-tree-id>/order.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
The following sections discuss how these navigation rules work for each page.
Navigating to the Search Page
Every page allows users to search for a product. Therefore, any page can potentially navigate to the search.jsp page, and this is indicated by the following navigation-rule tag:
<navigation-rule>
<from-tree-id>*</from-tree-id>
<navigation-case>
<from-outcome>search</from-outcome>
<to-tree-id>/search.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
| Page | JSP Filename |
|---|
| Main page | index.jsp |
| Search page | search.jsp |
| Browse page | browse.jsp |
| Product details page | details.jsp |
| Shopping cart page | shoppingCart.jsp |
| Check out page | checkOut.jsp |
| Order page | order.jsp |
Table 14-5 The JSP Filenames for BuyDirect Application Pages that Require Navigation Rules
Navigating to the Product Details Page
From the list of products in both the search page and browse page, users can click the Details hyperlink to view the details of a specific product. Therefore, you need the following two navigation rules:
<navigation-rule>
<from-tree-id>/search.jsp</from-tree-id>
<navigation-case>
<from-outcome>detail</from-outcome>
<to-tree-id>/details.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-tree-id>/browse.jsp</from-tree-id>
<navigation-case>
<from-outcome>detail</from-outcome>
<to-tree-id>/details.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
Navigating to the Shopping Cart Page
After viewing the details of a product, users can decide to buy that product. Therefore, the details page must be able to navigate to the shopping cart page. This is made possible by the following navigation-rule tag:
<navigation-rule>
<from-tree-id>/details.jsp</from-tree-id>
<navigation-case>
<from-outcome>buy</from-outcome>
<to-tree-id>/shoppingCart.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
Navigating to the Check Out and Order Pages
From the shopping cart page, users can check out. The following navigation-rule tag defines the transition from the shopping cart page to the check out page.
<navigation-rule>
<from-tree-id>/shoppingCart.jsp</from-tree-id>
<navigation-case>
<from-outcome>checkOut</from-outcome>
<to-tree-id>/checkOut.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
Finally, after users enter their delivery and payment details, they can pay. This requires navigation from the check out page to the order page, enabled by the following navigation-rule tag:
<navigation-rule>
<from-tree-id>/checkOut.jsp</from-tree-id>
<navigation-case>
<from-outcome>order</from-outcome>
<to-tree-id>/order.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
-----------------------------------------------------------
This article is excerpted from JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837). Check it out at your favorite bookstore today. Buy this book now. |
Next: Adding the ActionListener >>
More Java Articles
More By McGraw-Hill/Osborne