If you want to build an online store application using Java, look no further. This article begins with an overview of the application, and then discusses the applicatin development process. It is taken from chapter 14 of JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837).
Online Store Application - The MenuBean Class (Page 5 of 11 )
The menu box to the left side of each Web page contains hyperlinks that users can click to browse products by category. The hyperlinks are obtained from the instance of the MenuBean class, shown in Listing 14-5.
The MenuBean class has three properties: menu, browsePage, and lastUpdate. The menu property is read-only, and its value is a collection of hyperlinks in a table row. Each hyperlink has this format:
where [browse page] is the JSP page that lists products by category and is indicated by the browsePage property. By default, the value of the browsePage property is browse.jsp. As an example, the first category (with category identifier 1) is Digital Camera. Therefore, the hyperlink for this category is as follows:
The MenuBean class provides the value for its menu property to all of the application users. The menu property gets its value from the Categories table and does not change unless the content of the Categories table is modified. Therefore, to save resources, we employ a caching mechanism. The menu property will update its value for every specific interval only. The last update time is stored in the lastUpdate variable. When called, the getMenu method will compare the present time with the lastUpdate value. If the menu property value needs to be updated, the updateMenu private method is called.
public String getMenu() { long now = System.currentTimeMillis(); if (now > lastUpdate + updateInterval * 60 * 1000) updateMenu(); return menu; }
The updateMenu method obtains the DatabaseUtil instance from the ServletContext attribute whose key is DATABASE_UTIL and calls the getCategories method of the DatabaseUtil class:
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public float getPrice() { return price; }
public void setPrice(float price) { this.price = price; } }
The ProductDetailsBean Class
The ProductDetailsBean class extends the ProductBean class and adds two more properties: imageDir and imageUrl. The imageDir property specifies the directory under the application directory where all of the image files are stored. The imageUrl property specifies the URL to the image file for this product. The ProductDetailsBean class is shown in Listing 14-7.
public void setImageUrl(String url) { this.imageUrl = url; } }
The ProductDetailsBean provides a product’s details. The constructor of this bean is called when the JSF implementation instantiates this class. The constructor retrieves the information about the product from the Products table by calling the getProductDetails method of the DatabaseUtil class, and then populates all of its properties. The product identifier is obtained from the Request object in the ExternalContext object in the FacesContext object. Therefore, the page that uses the ProductDetailsBean must be called by passing a parameter called productId. Here is the part of the ProductDetailsBean class’s constructor that extracts the product identifier from the productId parameter:
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.