How to inject a ManagedBean into a ManagedBean

That's really easy, just use the @ManagedProperty annotation.

A Simple Example:

We got two managed beans, one for logging into the system and storing the user's state, and one for using a shopping cart. We got the LoginController

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "loginController")
@SessionScoped
public class LoginController {
	... // Do something...
}

and the ShoppingCartController, where we want to use the LoginController:

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class ShoppingCartController {
	
	// Inject some Enterprise Java bean (has nothing to do with the example)
	@EJB
	private BookingBean bookingBean;

	//Inject the LoginController (that's what we wanted to do )
	@ManagedProperty("#{loginController}")
	LoginController loginController;

	... // do the shopping cart stuff
}

That is easy. If you know how :)

Trackbacks

    Keine Trackbacks

Kommentare

Ansicht der Kommentare: (Linear | Verschachtelt)

  1. Little Helper schreibt:

    You can use a ManagedProperty....

    something like:

    @ManagedProperty(value="#{loginController}")
    private LoginController login;

    don't forget the getter/setter for this Property.

    this is the only method i know, doesn't mean that there is no other, better solution.


Kommentar schreiben



Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA