tags:

views:

69

answers:

1

Hey guys,

I have a basic question about JSF and their ManagedBeans. Imagine we have set a ManagedBean that only contains data for and from my view:

@ManagedBean(name = "dataBean")
@SessionScoped
public class DataBean {

Next here is my controller with a dependency injection to connect controller with model:

@ManagedBean
@RequestScoped
public class Controller {
  @ManagedProperty(value = "#{dataBean}")
  private DataBean dataBean;

Now let's think about several managedBeans in one controller: Is it possible to inject another ManagedBean to the controller like:

@ManagedProperty(value = "#{dataBean}")
private DataBean dataBean;
@ManagedProperty(value = "#{dataBean}")
private DataBean dataBean2;

And if so, how does JSF know what bean belongs to what .xhtml page?


I am very confused at the moment :-/ Because there is something wrong with my code... But first I should understand it clearly

Cheers...

Addition:

Let's say I want to access the same managedBean (sessionScoped) out of two controllers. Can I easy say:

@ManagedBean
@RequestScoped
public class Controller {
  @ManagedProperty(value = "#{dataBean}")
  private DataBean dataBean;
...
@ManagedBean
@RequestScoped
public class Controller2 {
  @ManagedProperty(value = "#{dataBean}")
  private DataBean dataBean;

And I have the same instance of DataBean?

+1  A: 

This code:

@ManagedProperty(value = "#{dataBean}")
private DataBean dataBean;
@ManagedProperty(value = "#{dataBean}")
private DataBean dataBean2;

Will inject the same instance in dataBean and dataBean2. There is only one #{dataBean}.

Addition (answer for question Addition :) )

Yes, if dataBean is @SessionScoped, in both controllers you get the same DataBean. There is only one in current session.

amorfis
all right: that's why i should use a controller for any view... thank's
Sven