views:

52

answers:

2

This is more of a general question, so that I a better understand about JSF and managed bean. So when people having multiple managed bean, is it solely because they want to group methods with the same scoped(RequestScoped, ApplicationScoped, SessionScope...) together? Or is there some other reason? What would happen if you inject managed bean with different scope into another? and why would they want to inject one bean with different scope to another, can I not just from JSF access different bean base on my need instead of inject one bean to another?

+1  A: 

I'm fairly new to JSF, but I have to yet see a managed bean inside a managed bean. Personally I have several managed beans in my appliaction, because each one is supposed to be a controller for a different view (some are Session Scoped, most of them are Request scoped and one or two are Conversation scoped). Let's say I have a user registration form, then I create a UserRegistrationController which is a managed bean for that view, it has a POJO like lets say UserRegistration with all the properties that are used in the view, and some methods like "registerUser()" which then pushes it to a service which takes care of all the work.

Zenzen
So from your JSF, you would call different managed bean depend on your need, correct?
Harry Pham
Yes. Like in the example in my JSF file i'd do something like #{userRegistrationController.form.name} where UserRegistrationController is the Bean, form is a POJO with all the properties like for example "name" (you can put them in the bean I guess but I just found it clearer and more reusable to put them in a new class). If I have another view lets say addGoods.jsf then I'd do another bean AddGoodsController etc. Obviously I have also some "multipurpose" beans which aren't really related to a single view.
Zenzen
+1  A: 

So when people having multiple managed bean, is it solely because they want to group methods with the same scoped(RequestScoped, ApplicationScoped, SessionScope...) together? Or is there some other reason?

That depends on the data the bean holds and the responsibilities of the bean. If it holds request scoped data (e.g. form input values and form actions), then the bean should go in request scope. If it holds session scoped data (e.g. logged-in user, user-specific settings like locale, etc), then the bean should go in session scope. If it holds application scoped data (e.g. dropdown values and other application wide constants), then the bean should go in application scope. It makes logically sense.

What would happen if you inject managed bean with different scope into another?

You can only inject a bean of the same or a broader scope in another bean. E.g. request, session or application scoped bean can be injected in a request scoped bean. JSF just sets it as a property of the target bean. This has no side effects. JSF will already error when you attempt to inject a bean of a narrower scope in another bean. You cannot inject a request scoped bean in a session scoped one. It makes perfectly sense. There can be more than one request scoped bean inside the same session, which one should JSF pick?

and why would they want to inject one bean with different scope to another, can I not just from JSF access different bean base on my need instead of inject one bean to another?

It'll only add nasty boilerplate code. Why not just let JSF handle it transparently? There's no need to take over JSF's responsibilities in your own hands. Injecting is cheap, even if you don't need the injected bean in the particular use case, it won't harm.

BalusC
About the thing you said about handle JSF transparently, let me confirm if I understand you correctly: So, in most cases, we should have JSF calling one main managed bean (ApplicationScoped or SessionScoped) only, and inject other `RequestScoped` beans into the main managed bean?
Harry Pham
No, you cannot inject a bean of a narrower scope in another bean. You can only inject application scoped beans in an application scoped bean. You can only inject application or session scoped beans in a session scoped bean. You can inject beans of all scopes in a request scoped bean. After all, you normally have a request (or view) scoped bean per view. There you inject the session or application scoped beans in whenever needed.
BalusC
make sense. tyvm
Harry Pham
You're welcome.
BalusC
Hi BalusC, I know this is an old topic, but I got a question related to what u said about letting `JSF handle it transparently`. If I have 2 bean `A` and `B`. `A` is SessionScoped and have method `foo()`. When I at `B.jsf`, I have a need to access method `foo()` from `A`, should I, from `B.jsf`, just access `foo()`via EL like `#{A.foo()}`, or should I inject `A` into `B`, and in `B` create `foo(){A.foo()}`. If i understand your post correctly, you were suggesting the second method with bean injection, correct?
Harry Pham