Advertise with us

Search Mastertheboss

Our eBooks

Java EE 6 example application


Building the JSF Managed Beans

Creating managed beans has never bean easier: you don't need any more declaring them in faces-config.xml. It's enough to declare them as annotation with the @ManagedBean annotation.

The first bean we will write is the @RequestScoped StoreManager JSF Bean which acts as a facade between the JSF page and the EJBs.

java ee 6 tutorial jboss 6

package control;

import java.util.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;

import view.CustomerView;
import view.ItemView;

import ejb.StoreManagerDao;

import model.Customer;
import model.Item;

public class StoreManager  {

 private StoreManagerDao storeManager;

 private CustomerView customerView;

 private ItemView itemView;

 List<Item> listOrders;
 List <SelectItem> listCustomers;

 public StoreManager() {     }

// Getters and setters here ...

 public void findOrders() {  

   listOrders = storeManager.findAllItems(customerView.getCustomerId());

 public void findAllCustomers() {  

   List<Customer> listCustomersEJB = storeManager.findAllCustomers();

   for (int ii=0;ii<listCustomersEJB.size();ii++) {
     Customer customer = listCustomersEJB.get(ii);
     listCustomers.add(new  SelectItem(customer.getId(),customer.getName()));


 public void saveOrder() { 


   FacesMessage fm = new FacesMessage("Saved order for "+itemView.getOrderQuantity()+ " of "+itemView.getOrderProduct());
   FacesContext.getCurrentInstance().addMessage("Message", fm);


 public void insertCustomer() {
   storeManager.createCustomer(customerView.getCustomerCountry(), customerView.getCustomerName());

   FacesMessage fm = new

      FacesMessage("Created Customer  "+customerView.getCustomerName()+ " from   "+customerView.getCustomerCountry());
   FacesContext.getCurrentInstance().addMessage("Message", fm);

   // Forces customer reloading

 public List<SelectItem> getListCustomers() {
   if (listCustomers == null) {
     listCustomers= new ArrayList();
   return listCustomers;

 public void setListCustomers(List<SelectItem> listCustomers) {
   this.listCustomers = listCustomers;

Things to notice:

1) The StoreManagerDao EJB is injected into the JSF Managed Bean using the annotation

private StoreManagerDao storeManager;

2) The other Managed Beans used in this project (to handle the form attributes) are injected using the @ManagedProperty annotation:

private CustomerView customerView;

private ItemView itemView;


© 2021 All Rights Reserved.

Please publish modules in offcanvas position.