tags:

views:

621

answers:

3

I have done some reading and playing and I still have some questions I was hoping someone might answer:

So, can I use two or more backing beans in a single JSF page?

<h:intputText value="#{myFirstBean.firstProperty}" />
<h:intputText value="#{mySecondBean.secondProperty}" />

If I can, why should I not do it? (I assume I should not, because nobody does)

If I cannot, why?

Also, I read somewhere something like "on page load the framework would instantiate the backing bean, and populate it if it's a postback". They say the backing bean but I cannot understand how the framework know which backing bean to instantiate.

+1  A: 

Why not? It's a perfectly legitimate thing to do. Generally, a page should be associated with one bean (for the sake of good structure), but if you want, for example, to show the current time on each page, you are free to reference your timeBean.currentTime, among other things (of course, using include/templating is preferable here).

Bozho
I am still trying to understand how the framework decides which beans to instantiate when displaying a page in a GET request. I think it would be correct to ask "when" does the bean gets instantiated? I'd have to assume on first access..
Nir Levy
Managed beans are created by framework when are used in displayed page. Of course we are talking of request scope beans and session beans that don't exits yet.
cetnar
+1  A: 

Let's clarify some terms:

  • managed beans are JavaBeans components that you can configure using the managed bean facility see
  • backing beans, are a JavaServer Faces managed beans that are associated with the UI components used in a particular page see

So, yes you can use two or more managed beans in a single JSF page, but splitting UI components bindings, listeners, logic etc. that are related to one page into two or more backing beans is still posible but very undesirable and might cause you lot of problems and bad code.

cetnar
This definition from Sun is fairly opaque. I'd rather say: a backing bean is a Java (model) class which is associated with a view, a managed bean is an *instance* of this class which is associated with a *certain* view and scope.
BalusC
Indeed. Many developers confuse both terms. I use word 'backing bean' in situation when this class is created on purpose of jsf page. For other beans (like data model beans) I use term manage beans.
cetnar
A: 

Other questions have already been answered. However:

Also, I read somewhere something like "on page load the framework would instantiate the backing bean, and populate it if it's a postback". They say the backing bean but I cannot understand how the framework know which backing bean to instantiate.

Beans are resolved by their name. For example, #{myFirstBean.firstProperty} looks for bean named 'myFirstBean' (an instance of class MyFirstBean). You can adjust the name as in:

@ManagedBean(name = "foo")
public class SomeClass { ... }

Then you could reference that via #{foo.firstProperty}.

Tuukka Mustonen