Previous Next vertical dots separating previous/next from contents/index/pdf

11. Test the Application

In this step, we will complete the web project by importing some web components like DAO classes, JSPs and servlets. We will also create a server configurations, and deploy and run the Hibernate web application.

The tasks in this step are:

To Import and Edit Web Components (JSP and DAOs and so on.)

The following task adds the necessary packages, JSPs, and DAOs to complete your Hibernate web application for the purpose of this tutorial.

  1. Create the following java packages in the workshop-hibernate-tutorial/web/Resources/dao-classes folder:
  2. Copy all the classes from the workshop-hibernate-tutorial/web/Resources/dao-classes folder to com.bea.dao package. Classes under this package manages object persistence using Hibernate.
  3. Similarly, the MessageFactory class from the workshop-hibernate-tutorial/web/Resources/util folder to com.bea.util package.
  4. Copy all the JSPs from the workshop-hibernate-tutorial/web/Resources/JSPs folder to workshop-hibernate-tutorial/web/pages folder.
  5. Copy the workshop-hibernate-tutorial/web/Resources/stylesheet.css file to the web folder. (Switch to the Project Explorer view for this step. )
  6. Copy the workshop-hibernate-tutorial/web/Resources/application.properties file and overwrite to application.properties file under the web/WEB-INF/src/java/resources folder.
  7. Create a new class CustomerManagedBean under the com.bea.beans package. Add property customer of type com.bea.beans.Customer to CustomerManagedBean class and corresponding getter / setter methods.
  8. We will also add few action methods to com.bea.beans.CustomerManagedBean.java. The methods are responsible to add the customer to the data store, retrieve orders of specific customer.
  9. In CustomerManagedBean class, copy methods from workshop-hibernate-tutorial/web/Resources/BackingBean_Customer_Methods.txt file.
        public String addCustomer( ) {
            
            FacesContext context = FacesContext.getCurrentInstance( ); 
            ExternalContext extContext = context.getExternalContext( ); 
            
            DAOFactory daoFactory = new DAOFactory( );
            CustomerDAO customerDAO = daoFactory.getCustomerDAO( );
            
            try {
                customerDAO.addCustomer(customer);
                
                Object[ ] objArr = new Object[ ] { customer.getName( )  , customer.getCustomerid( ) }; 
                FacesMessage message = MessageFactory.getMessage(context, "Successful_CustomerRegistration", objArr); 
                String msg = message.getDetail( ); 
                extContext.getRequestMap( ).put("Successful_CustomerRegistration", msg); 
                return "success"; 
                
            } catch(DAOException ex) {
                Object[ ] objArr = new Object[ ] { ex.toString() };
                FacesMessage message = MessageFactory.getMessage(context, "Error_AddingCustomer", objArr);
                context.addMessage(null, message);
                return null;
            }
            
        }
        
        public String viewOrders( ) {
            
            FacesContext context = FacesContext.getCurrentInstance( ); 
            ExternalContext extContext = context.getExternalContext( ); 
            
            DAOFactory daoFactory = new DAOFactory( );
            CustomerDAO customerDAO = daoFactory.getCustomerDAO( );
            OrderDAO orderDAO = daoFactory.getOrderDAO( );
            
            try {
                Customer customerBean = customerDAO.getCustomer(customer.getCustomerid( ).intValue( ));
                OrderData[ ] orders = orderDAO.getOrderByCustomer(customer.getCustomerid( ).intValue( ));
                if(orders.length > 0) {
                    extContext.getRequestMap( ).put("customerName", customerBean.getName()); 
                    extContext.getRequestMap( ).put("orders", orders); 
                    return "ordersList"; 
                } else {
                    Object[ ] objArr = new Object[ ] { customerBean.getName() };
                    FacesMessage message = MessageFactory.getMessage(context, "Error_NoOrders", objArr);
                    context.addMessage(null, message);
                    return null; 
                }
                
            } catch(DAOException ex) {
                Object[ ] objArr = new Object[ ] { ex.toString() };
                FacesMessage message = MessageFactory.getMessage(context, "Error_GettingOrders", objArr);
                context.addMessage(null, message);
                return null;
            }
            
        }
  10. Using the bulb icon import following packages :
  11. Save CustomerManagedBean.java file.

Edit the Faces Deployment Descriptor

  1. You need to define Managed Beans and Navigation Rules in faces-config.xml file.
  2. The web/Resources folder contains faces-config.xml file with Managed Bean configurations and Navigation Rules for the web application. Copy (and overwrite) web/Resources/faces-config.xml to web/WEB-INF/config folder.
  3. Open the web/WEB-INF/config/faces-config.xml file and review the Faces configurations. It defines Customer and CustomerManagedBean as managed beans in request scope. Also, defines DAOFactory as managed bean in application scope.

  4. It defines Navigation Rules for addCustomer.jsp and viewAllCustomers.jsp. The navigation case for addCustomer.jsp specifies /pages/viewAllCustomers.jsp as destination page while the outcome is success.

  5. Similarly, it defines the navigation case for viewAllCustomers.jsp describing /pages/viewOrders.jsp as destination page while the outcome is orderList.

  6. Save the faces-config.xml file.

Create a Server Configuration

Workshop has the ability to run and debug applications on most of the popular web containers that are in use today. Before you can run this sample application, you must have a server installed.

  1. Click on the Servers tab.
  2. Right click on the Servers tab and choose New > Server.

  3. Choose the type of server that you have installed. Click Next.

  4. Select the project name in the left pane and click Add to move it to the right pane. Click Finish.

  5. The new server is displayed in the Servers view.

Deploy the Hibernate Web Application

  1. To run the application, right click on the project name in the Project Explorer and choose Run As > Run on Server from the submenu.

    Click Finish.

  2. Once the server starts successfully, the test browser pane opens. Enter the address http://localhost:8080/pages/addCustomer.jsf in the address line of the test browser pane (http://localhost:7001/pages/addCustomer.jsf if you are running on WebLogic Server). The Console view displays the server output. Once the Tomcat server starts completely, the addCustomer.jsf page is called in browser.
  3. If any deployment specific errors are found then resolve them and ensure successful deployment of the application.

    It may be necessary to update the hsqlDB folder for a successful deployment. To update the hsqlDB folder: switch to the AppXplorer view, right-click workshop-hibernate-tutorial/web/hsqlDB and select Refresh.

To Run the Application

  1. Once the addCustomer JSF page loads, you will see the screen below.

  2. To add a new customer, enter name Bob and click Submit.

    The application calls addCustomer( ) action method of Customer managed bean and forwards the request to viewAllCustomers.jsp, if customer is added successfully. The viewAllCustomers.jsp gets customers list using <h:dataTable> component and displays it shown below

  3. You can also observe the debugging messages at the Console view in Workshop. Note: we have enabled the hibernate.show_sql property to display SQL statements.

  4. Click View Orders button for the customer name JOHN to get the list of order(s) placed by JOHN.

  5. Click Back to browse the previous page and get list of orders placed by other customers.
  6. Once you are done, stop the Tomcat Server.

Click one of the following arrows to navigate through the tutorial:


Still need help? Post a question on the Workshop newsgroup.

 

Skip navigation bar   Back to Top