JSF multiple backing beans on one page
Asked Answered
I

3

9

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.

Impetrate answered 10/12, 2009 at 19:41 Comment(0)
P
6

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.

Peregrinate answered 10/12, 2009 at 20:14 Comment(2)
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.Antheridium
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.Peregrinate
A
4

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).

Audryaudrye answered 10/12, 2009 at 20:8 Comment(2)
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..Impetrate
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.Peregrinate
S
3

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}.

Singleness answered 18/8, 2010 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.