Search code examples
javamodel-view-controllerdesign-patternsjsf

What components are MVC in JSF MVC framework?


In JSF MVC framework who is Model, View, and Controller?


Solution

  • This depends on the point of view (pun intented).

    In the big architectural picture, your own JSF code is the V:

    M - Business domain/Service layer (e.g. EJB/JPA/DAO)
    V - Your JSF code
    C - FacesServlet

    In the developer picture, the architectural V is in turn dividable as below:

    M - Entity
    V - Facelets/JSP page
    C - Managed bean

    In the smaller client picture, the developer V is in turn dividable as below:

    M - JSF component tree
    V - Rendered HTML output
    C - Client (webbrowser)

    In the yet smaller JavaScript picture, the client V is in turn dividable as below:

    M - HTML DOM tree
    V - Visual presentation
    C - Event listener functions (enduser interaction and Ajax)

    So it's basically a M(M(M(MVC)C)C)C ;)

    Note that some starters and even some —very basic— tutorials mingle/copy/flatten the entity's properties in the managed bean, which would effectively make the controller a model. Needless to say that this is poor design (i.e. not a clean MVC design).

    The code snippets in the following answers illustrate the right MVC approach:

    In the books The Definitive Guide to JSF in Java EE 8, chapter 8 "Backing beans", page 276, and The Definitive Guide to Jakarta Faces in Jakarta EE 10, chapter 8 "Backing Beans", page 288, the below Venn diagram is used to illustrate the position of the backing bean in the MVC paradigm within the context relevant to the JSF developer. Copyright disclaimer: aforementioned books are written by me and the picture is created by me.

    enter image description here