Book Image

Mastering JavaServer Faces 2.2

By : Anghel Leonard
Book Image

Mastering JavaServer Faces 2.2

By: Anghel Leonard

Overview of this book

Table of Contents (20 chapters)
Mastering JavaServer Faces 2.2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The JSF Life Cycle
Index

Managed bean communication


Until now, we have focused especially on the communication between Facelets and managed beans. In this section, we will cover another important aspect regarding JSF communication—managed beans communication. We will discuss the following topics:

  • Injecting a managed bean into another bean

  • Communication between managed beans using the application/session map

  • Accessing other managed beans programmatically

Injecting a managed bean into another bean

A managed bean can be injected into another managed bean using @ManagedProperty. For example, let's suppose that you have a managed bean in the session scope that stores a player name and surname, as shown in the following code:

@Named
@SessionScoped
public class PlayersBean implements Serializable{

  private String playerName;
  private String playerSurname;

  public PlayersBean() {
    playerName = "Rafael";
    playerSurname = "Nadal";
  }

//getters and setters
}

Now, let's suppose that you want to have access to this bean...