Step 2. Create JSF Manage Beans

Time to complete this step: 10 minutes

To enable the JSF framework for instantiating backing beans and to store them in the appropriate scope, you are required to configure it in the application configuration resource file faces-config.xml using the managed-bean XML element. The faces-config.xml file is processed at application startup time. When a page references a bean, the JavaServer Faces implementation initializes it according to its configuration in the application configuration resource file.

OEPE provides a multi-page Faces-Config Editor for configuring various components of JSF. Also, it provides a wizard to create a backing bean and configure it as a managed bean in the faces-config.xml file.

In this step, you will create the backing beans, ProductInfoBean and NewTicketBean; and configure them as managed beans. 

You will perform the following tasks in this step:


Create and configure a managed bean - ProductInfoBean  

In this step, you will create a new JSF backing bean,  ProductInfoBean. The  ProductInfoBean provides list of products for which the Trouble Ticket System can accept tickets. Since, it's a generic bean providing some information, we will configure it to the application scope.

  1. Open the  TroubleTicketSystemWebClient > WebContent > WEB-INF > faces-config.xml
  2. Switch to the ManagedBean page. Click the Add button to define a new managed bean. That opens the New Managed Bean Wizard. Since, you are creating a new backing bean to be configured as a managed bean with the JSF framework, choose the Create a new Java class option. 

  3. Click Next.
  4. In the New Java Class wizard page, choose the com.oracle.ticketsystem.jsf.beans package and enter ProductInfoBean as the class name.

  5. Click Next.
  6. The  Managed Bean Configuration wizard page allows you to set the name, scope and description of the managed bean. Enter/choose the following parameters:
    • Name: productInfo
    • Scope: application
    • Description: Provides read-only information about Trouble Ticket System products.

  7. Click Next.
  8. The Wizard Summary page describes the options you have entered/selected during the wizard flow.

  9. Click Finish.
  10. That creates the  com.oracle.ticketsystem.jsf.beans.ProductInfoBean class and configures it as JSF managed bean.

  11. Save the faces-config.xml file.
  12. You can switch to the Source page of Faces-Config Editor and verify that the configuration has been added.
  13. On the ManagedBean page, click the Managed Bean class link for the productInfo bean to open the  com.oracle.ticketsystem.jsf.beans.ProductInfoBean class.
  14. Since, the  ProductInfoBean is responsible for providing a list of available products, you need to implement the  getProducts()  method as shown below. The  getProducts() method makes a web service call to the ProductWebService and retrieves all the products. Since, the  getProducts() method will be called to render a list of products in the JSF UI component, it shall return a list of JSF  javax.faces.model.SelectItem instances. Add any runtime failures to the JSF  FacesContext

     public List<SelectItem> getProducts() {

      FacesContext context = FacesContext.getCurrentInstance();
      List<SelectItem> productItems = new ArrayList<SelectItem>();
      try {
       ProductWebService productWebService = WebServiceClientFactory.getProductWebService();
       
       ProductResultType productResultType = productWebService.getAllProducts();
       
       for(ProductType product : productResultType.getProducts()) {
        SelectItem item = new SelectItem(product.getId(), product.getName());
        productItems.add(item);
       }
       
       return productItems;
       
       
      } catch (TicketSystemException e) {
          FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Error finding products!!",
                        e.getMessage());
          context.addMessage(null, message);
          return null;
      }
      
     }



  15. Save the  ProductInfoBean class.

 

Create and configure a managed bean - NewTicketBean  

In this step, you will create a new JSF backing bean,  NewTicketBean.  The  NewTicketBean accepts a new ticket request and creates a new ticket based on input parameters and returns the created ticket as result. Since, the  NewTicketBean is handling new ticket requests and is specific to a user, we will configure it to the request scope.

  1. Open the  TroubleTicketSystemWebClient > WebContent > WEB-INF > faces-config.xml
  2. Switch to the ManagedBean page of the Faces-Config Editor. Follow similar steps that you performed in the previous step to add a new managed bean class with following parameters:
    • Class:  com.oracle.ticketsystem.jsf.beans.NewTicketBean
    • ManagedBean Name: newTicketBean
    • Scope: request
    • Descrption: A managed bean for handling new ticket requests.
    Note: The NewTicketBean has been set with the request scope so that a new instance shall be created by the JSF framework for every request.
  3. Save the faces-config.xml file.
  4. Open the  com.oracle.ticketsystem.jsf.beans.NewTicketBean class.
  5. Declare the following variables in the  NewTicketBean class.

     private long productId;
     private String customerName;
     private String customerEmail;
     private String title;
     private String description;



  6. Generate getter and setter methods for each variable (Hint: Right-click and select Source > Generate Getters and Setters...).
  7. Since, the  NewTicketBean  is responsible for creating a new ticket, you will implement a  create()  method. The  create() method submits a web service call to the TicketWebService for creating the new ticket and returns a "success" outcome, if the ticket has created successfully. It shall put the  TicketType instance to the JSF  ExternalContext request map and submit an appropriate message to the JSF  FacesContext using the  MessageFactory helper class. For any failure, it adds a failure message to the  FacesContext and returns null

     public String create() {
      
      TicketWebService ticketWebService = WebServiceClientFactory.getTicketWebService();
      FacesContext context = FacesContext.getCurrentInstance();
      try {
       TicketType ticketType = ticketWebService.add(getProductId(), getCustomerName(), getCustomerEmail(), getTitle(), getDescription());
       context.getExternalContext().getRequestMap().put("ticketType", ticketType);
       
       Object[ ] objArr = new Object[ ] { ticketType.getTitle(), new Long(ticketType.getId()) };
       
       FacesMessage message = MessageFactory.getMessage(context, "newTicketResult.message", objArr);
       context.addMessage(null, message);
       
       return "success";
       
      } catch (TicketSystemException e) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Error creating a new ticket!!",
                        e.getMessage());
                context.addMessage(null, message);
                return null;
      }
     }



  8. Save the  NewTicketBean class.

 

Click the arrow below to navigate through the tutorial: