JSF examples of learning - Pisa (pizza) ordering system.
Abstract: JSF examples of learning - Pisa (pizza) ordering system.
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
JavaServer Face for the Java application development speed.
JavaServer Faces (JSF) is a Java technology used to quickly build Web applications of the new technology. JSF through the provision of the following features to speed up the development process:
Standards and scalable user interface components; vulnerable configuration pages navigation; certification for the importation of components; automation Bean management; incident; easily error handling,
And built-in support of the international.
This paper describes how to use JSF to build online Pisa (pizza) ordering system.
Job description
class=bodycopy> The application is called PizzaRia is an online store that allows users to choose pizza and submit selected pizza. PizzaRia similar with other online stores,
Users can browse the menu, add to the shopping cart and checkout selected products.
The application's user interface from five JSP document composition, they are index.jsp, details.jsp, shoppingCart.jsp, checkOut.jsp and order.jsp.
Each user interface pages, including three other pages: header.jsp, menu.jsp and footer.jsp
Users shopping process: the store to view the goods detailed information, shopping (Add to Cart), checkout, payment.
<table Width="100%" align=center> <tr> <td align=middle> </ td> </ tr> </ table><table Width="100%" align=center> <tr> <td align=middle> </ td> </ tr> </ table>
<table Width="100%" align=center> <tr> <td align=middle> </ td> </ tr> </ table>
<table Width="100%" align=center> <tr> <td align=middle> </ td> </ tr> </ table> <table width="100%" align=center> <tr> <td height = "453" align = middle> </ td> </ tr> </ table>
Database
The procedure of data stored in three forms: products (products), orders (orders) and OrderDetails (orders detailed project). Products Table information storage products, has four: ProductId (product logo), Name (name), Description (Note), and Price (prices).
class=bodycopy style1> Orders table for each row in a separate storage of orders, the information including contact names, addresses and credit card details of delivery. Orders Table 6 are:
OrderId (orders logo), ContactName (contact name), DeliveryAddress (postal address), CCName (credit card belonging name),
CCNumber (credit card numbers) and CCExpiryDate (credit card expiration date).
Each of the detailed project orders in the OrderDetails be stored in the table. OrderDetails table with four: OrderId (orders logo), ProductId (product logo),
Quantity (quantity) and Price (prices). Orders and OrderDetails through OrderID table included one-to-many relationship. Please note that the user table OrderDetails orders on the preservation of the relevant price information. The prices may be different with the current product prices, which stored in the table Products Price column.
In an Oracle database used to create the necessary tables script files stored in pizzaria-oracle.sql pizzaria.zip document.
Business Object
Following is the application used in the business object:
ProductBean package for a product information. It has the following attributes: id (logo), the name (name), description (Note), and price (prices).
Details.jsp each page visited, JSF Implementation (implementation) will automatically create a ProductBean examples. The JSF Implementation call ProductBean
No parameters of the structure, from the database access to the relevant data, and will fill its corresponding word column.
ProductSummary. ProductSummary (Product Overview) for a summary of that product. These include two attributes: id (identification) and the name (name).
ShoppingItemBean. ShoppingItemBean shopping for that item. These include four attributes: productId (product logo), productName
(Product name), the price (price), as well as quantity (number).
ShoppingCartBean. ShoppingCartBean used for storage in a dialogue (session) objects in the cart. Allows users to add such items (using addShopping methods), including all access to a list of items (using getShoppingItems method), and was purchased by the total value of goods (used getTotal methods).
OrderBean. OrderBean said that one orders. Such has the following five attributes: contactName, deliveryAddress, creditCardName,
CreditCardNumber and creditCardExpiryDate.
MenuBean. MenuBean getMenu method showed that the use of products for the directory. The method returns a product contains details of the link to the HTML table.
Package pizzaria;
Import java.util.ArrayList;
Import java.util.Iterator;
Import javax.faces.context.FacesContext;
Import javax.servlet.ServletContext;
(Public class MenuBean
String browsePage = "details.jsp";
Public String getBrowsePage () (
Return browsePage;
)
Public void setBrowsePage (String page) (
BrowsePage = page;
)
Public String getMenu () (
/ / Get DatabaseUtil instance
FacesContext facesContext = FacesContext.getCurrentInstance ();
ServletContext servletContext = (ServletContext) facesContext.getExternalContext (). GetContext ();
DatabaseUtil dbUtil = (DatabaseUtil) servletContext.getAttribute ( "DATABASE_UTIL");
StringBuffer buffer = new StringBuffer (512);
Buffer.append ( "<table> \ n");
ArrayList summaries = dbUtil.getProductSummaries ();
= Summaries.iterator Iterator iterator ();
While (iterator.hasNext ()) (
ProductSummary summary = (ProductSummary) iterator.next ();
Buffer.append ( "<tr> <td>");
Buffer.append ( "" +
Summary.getName () + "");
Buffer.append ( "</ td> </ tr> \ n");
)
Buffer.append ( "</ table> \ n");
Return buffer.toString ();
)
)
DatabaseUtil. DatabaseUtil provided the following three kinds of methods to access and manipulate data:
- GetProductSummaries: The method returns a product contains a summary table in a list of all products. Overview of a product category to ProductSummary said.
- GetProductDetails: ProductBean the method returns an object, the object identifier package with a specific product details.
-
class=bodycopy> InsertOrder: The method to the Orders and OrderDetails Table Insert Table customer orders.
Application context sniffers
class=bodycopy style1> Application context sniffers (AppContextListener) read out from the web.xml file access the database for the initial parameters, and then be incorporated into
ServletContext object. The initial use parameters are as follows: jdbcDriver, dbUrl, dbUserName and dbPassword. In your web.xml file edit these values in order to reflect the true value of your database.
Package pizzaria; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class AppContextListener implements ServletContextListener (/ / web.xml file read Servlet context initialization parameters, construct a DatabaseUtil Object public void contextInitialized (ServletContextEvent event) (DatabaseUtil dbUtil = new DatabaseUtil (); ServletContext servletContext = event.getServletContext (); String jdbcDriver = servletContext.getInitParameter ( "jdbcDriver"); String dbUrl = servletContext.getInitParameter ( "dbUrl"); String dbUserName = servletContext.getInitParameter ( "dbUserName"); String dbPassword = servletContext.getInitParameter ( "dbPassword"); dbUtil.setJdbcDriver (jdbcDriver); dbUtil.setDbUrl (dbUrl); dbUtil.setDbUserName (dbUserName); dbUtil.setDbPassword (dbPassword ); servletContext.setAttribute ( "DATABASE_UTIL" dbUtil);) public void contextDestroyed (ServletContextEvent event) (ServletContext servletContext = event.getServletContext (); servletContext.removeAttribute ( "DATABASE_UTIL");))
JSF application configuration
class=bodycopy style1> JSF allows programmers only through the application process configuration files can be easily configured applications. If the document exists, then it should be named for the faces-config.xml, and should be in your application under the WEB-INF directory.
In the faces-config.xml file in the application's configuration in many aspects, including bean management, page navigation, custom UI (user interface) components,
Custom verification procedures and information resources. In PizzaRia application, I used the faces-config.xml bean management and navigation pages of the configuration.
<? Xml version = "1.0">
Faces-config PUBLIC
"- / / Sun Microsystems, Inc / / DTD JavaServer Faces Config 1.0 / / EN"
"Http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
Incident
In this application, developers need a AppAction Listener called the ActionListener. ProcessAction ActionEvent its way from the target getLocalValue acquire passed to the method of local values. If the local value of "Buy (purchase)," then processAction users with access to relevant ShoppingCartBean objects, and will shoppingItem to join in the bean. If the local value of "pay (payment)," then processAction from the session (session) objects and object access OrderBean ShoppingCartBean object, and call DatabaseUtil insertOrder object methods.
Package pizzaria; import java.util.Map; import javax.faces.FactoryFinder; import javax.faces.application.Application; import javax.faces.application.ApplicationFactory; import javax.faces.component.UICommand; import javax.faces.context . FacesContext; import javax.faces.el.ValueBinding; import javax.faces.event.ActionEvent; import javax.faces.event.ActionListener; import javax.servlet.ServletContext; public class AppActionListener implements ActionListener (public void processAction (ActionEvent event) = (FacesContext facesContext FacesContext.getCurrentInstance (); String localValue = (String) ((UICommand) event.getComponent ()). getValue (); if (the "Buy." equals (localValue)) (String productId = (String) facesContext . getExternalContext (). getRequestParameterMap (). get ( "myForm: productId"); Map sessionMap = facesContext.getExternalContext (). getSessionMap (); ShoppingCartBean cart = (ShoppingCartBean) sessionMap.get ( "shoppingCartBean"); if (cart = = null) (cart = new ShoppingCartBean ();// create Cart sessionMap.put ( "shoppingCartBean" cart);) = getDatabaseUtil ProductBean product (). getProductDetails (productId); ShoppingItemBean shoppingItem = new ShoppingItemBean (product.getId (), product.getName (), product.getPrice (), 1); cart.addShoppingItem (shoppingItem);) else if ( "Pay." equals (localValue)) (/ / insert a record into the database OrderBean order = (OrderBean) getValueBinding ( "# (orderBean)"). getValue (facesContext); ShoppingCartBean cart = (ShoppingCartBean) getValueBinding ( "# (shoppingCartBean)"). getValue (facesContext); if (cart! = null & order! = null ) (getDatabaseUtil (). insertOrder (order, the cart); / / empty shopping cart cart.removeShoppingItems ();))) private ValueBinding getValueBinding (String valueRef) (ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory (FactoryFinder.APPLICATION_FACTORY); Application application = factory.getApplication (); return application.createValueBinding (valueRef);) private DatabaseUtil getDatabaseUtil () = (FacesContext facesContext FacesContext.getCurrentInstance (); ServletContext servletContext = (ServletContext) facesContext.getExternalContext (). getContext (); return (DatabaseUtil) servletContext.getAttribute ( "DATABASE_UTIL");))
AppActionListener of the use of two very useful approach: getValueBinding and getDatabaseUtil. GetValueBinding accept the designated target of a string, and returns an object of type down conversion ValueBinding type of object. For example, in order to obtain user in the application configuration file to be registered as shoppingCartBean ShoppingCartBean example, developers need to transfer through "shoppingCartBean" to call getValueBinding.
ShoppingCartBean cart = (ShoppingCartBean) getValueBinding ( "# (shoppingCartBean)"). GetValue (facesContext);
GetDatabaseUtil method returns an DatabaseUtil ServletContext of the examples cited:
Private DatabaseUtil getDatabaseUtil () = (FacesContext facesContext FacesContext.getCurrentInstance (); ServletContext servletContext = (ServletContext) facesContext.getExternalContext (). GetContext (); return (DatabaseUtil) servletContext.getAttribute ( "DATABASE_UTIL");) operational procedures: http: / / 127.0.0.1:8080 / pizza / faces / index.jsp
The original author PizzaRia JSF applications using JavaServer Faces [JSF] 1.0. In order to facilitate learning, the station will use its jsf1.1 + Access, and rewrite all jsp document. Please download the source code analysis.
</ Td> </ tr> <tr> ↑ Back
Tags: examples, java jsf, java system, jsf, System






