Search code examples
javamodel-view-controllerjsf-2javabeansspring-roo

Confused: Role of Beans in JSF2 in comparison to classical MVC Controllers


I have a question that is more design and architecture related. I am coming from a classical MVC based background and have to get my hands dirty on JSF2. I read the IBM articles on JSF2 (http://www.ibm.com/developerworks/library/j-jsf1/) and think I understand the overall concept.

I started to get in touch with JSF2 through ROO. I have the feeling that ROO (maybe this is true for any JSF2-Type App, or maybe not) is making very strange/unclear use of beans. It is in general really not clear to me what the actual role of a Bean is! For example, if I have a view with a form that is for editing a single user-entry, i would initialize the user in a, lets call it UserBean (maybe store it in a member variable) and access this variable trough getters. If i now want to overview all users, I would again render the view in in the UserBean hold a collection of users and again access this collection through getters. The previous description is actually the way i would do things with jsf. This means I would user the UserBean more as a stateful-service as a controller.

In a typical controller situation i would create for every type of action (list user, edit user, view user, etc) a separate controller, with specific initialized data and this way i would separate the context of the logic by controllers.

I often use context-specific services, e.g., if I handle user's often an spread over the application, I create a user-service that handles user-specific logic that is maybe too complex to be put into itself. If I now for example look into roo generated Beans, I would find methods that programmatically render forms, input fields, and labels, that again store list's of users, Boolean fields that indicate if data had already been loaded, single user members and a lot of methods that more look like to be put into a UserService (or whatever). I am wondering if this is the way JSF2 is intended to be used, in words: pushing everything that is related to one context into on bean, not making use of service, and writing "super-controller-beans" that handle everything.

I don't really know if you get the question right, but what would maybe help me is, a hint to

  1. a very exemplary and commendable example application that makes use of beans the way they were intended to be used in combination with jsf2 features and usecases that for example implement basic CRUD usecases around a given type of entity. (One big confusing point is, that in my case ROO always makes use of AJAX and javascript stuff like Modal-Dialogs to implement CRUD logic. I wonder if with JSF there is a more classical way to to this? [With 'classical' I mean for example URl-based views and separated views for listing, editing, and viewing entities])
  2. a resource that enlightens typical "thats-the-way-the-good-guys-do-it" JSF-Patterns (maybe this is J2EE Patterns?).

Thank you so much!

Please feel free the push me to concretize specific points if I am not clear!


Solution

  • The link for JSF2 you have posted points to JSF1.2 article. In case you want to start of with JSF2 or JSF I suggest following links.

    I'll suggest start with plain vanilla JSF rather than ROO with JSF to get a hang of JSF.

    To answer your question

    • First link provides you with simple jsf examples, in JSF you can have both ajax based and classical way of submitting form. In JSF 1.x versions ajax was not part and parcel of JSF it was implemented by third party component library mainly RichFaces and PrimeFaces to name few. In JSF2 there is inbuilt support for ajax, this does not apply third party components are no longer required, they still provide some extended features. I'll suggest go through this link to find differences between JSF 1.x and JSF 2.
    • Patterns I am not aware of as such as specific to JSF apart code can be categorized in model - view - controller. Typical case Person represents model, PersonMangedBean plays role of controller which plays central role of getting data from view(jsp/facelets) and after processing data in bean itself or service beans handles navigation to classic views may be listPersons.xhtml.
    • JSF managed beans are not "super-controller-beans" handling every thing in that bean. I try to categorize things the way you mentioned i.e. have a service layer where we have all business logic may be EJB or Spring managed bean and it decouples at-least business logic away from view technology JSF whereby it(service) can be reused somewhere else as a library if designed properly.
    • Tip: JSF is component based framework not an action based and it has lifecycle of its own, do get a grip of that life-cycle will save lots of time and proper understanding of the framework. This link though for JSF 1.x holds good for JSF2 too, for basic understanding of life-cycle.

    Hope this helps.