101

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

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
yegor256
  • 102,010
  • 123
  • 446
  • 597

4 Answers4

158

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 Completely agree with both philosophy,generally it is more the case with big arch. view in real time projects. – jmj Feb 24 '11 at 12:55
  • 2
    didn't get the pun, still have a upvoat – Nick Russler Dec 05 '12 at 16:21
  • @JeffLee at my work, we see it as the managed beans control the xhtml view. Managed beans can talk to the model code to get and to save entities depending on what the view needs. – djeikyb Jul 15 '13 at 22:36
  • 5
    @Jeff: it's that only if you mingle the model properties in it. As in, using `` instead of ``. – BalusC Nov 15 '13 at 17:21
  • Totally agree with this answer to the detriment of @Jigar's one. IMO, managed beans are much more than a model. They're kind of extension of the FacesServlet, where all the interaction happens. Also they are able to hold state, but much more a C rather than a M. – Aritz Mar 26 '14 at 16:19
  • How come I'm using the @Model annotation in the Controller? – K.Nicholas Nov 13 '15 at 20:37
  • Thanks @BalusC. I totally agree. Just one consideration: when I read "picture" I started searching for something like an diagram (image) in the answers bodies. Haha. – Jaumzera Jun 09 '16 at 14:36
36

M odel would be your ManagedBean

V iew would be jsp,XHTML (well you can accommodate various views here )

C ontroller will be FacesServlet

Update, hope this picture helps more

enter image description here

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 4
    +1, Makes sense, although I would maybe say your Model is the Entities (if you have any) and the Controller is the Managed Beans (in addition to the FacesServlet) – Shervin Asgari Feb 24 '11 at 11:48
  • @Jigar well, if `FacesServlet` is a controller than I can't extend it. I can't have my own controllers... I would agree with Shervin that Managed Beans is Controller. Shervin, how about posting your own alternative answer? – yegor256 Feb 24 '11 at 12:08
  • 1
    it is undercover `FacesServlet` which is controlling. – jmj Feb 24 '11 at 12:09
  • @Jigar [wikipedia](http://en.wikipedia.org/wiki/Model–View–Controller) says that "A controller accepts input from the user and instructs the model and viewport to perform actions based on that input." I doubt that FacesServlet "instructs the model", does it? – yegor256 Feb 24 '11 at 12:13
  • 4
    Undercover FacesServlet receives all the data (at above layer it is called bean) from request,session,application context and processes it. – jmj Feb 24 '11 at 12:18
  • Check update. @Shervin I hope this picture describes it well. – jmj Feb 24 '11 at 12:24
  • @sergionni with my answer refresh your page – jmj Feb 24 '11 at 12:53
  • @Jigar i see "enter image description here" or could you,please,send link, i'm interested in this question – sergionni Feb 24 '11 at 13:05
  • [Source of image](http://publib.boulder.ibm.com/infocenter/ieduasst/rtnv1r0/index.jsp?topic=/com.ibm.iea.rad_v6/rad/6.0/JSF/WASv6_JSF_Details/player.html). – Buhake Sindi Nov 09 '12 at 08:56
4

The faces servlet manages the faces lifecycle so in that sense it is the controller combined with your own code that may get called during each lifecycle phase

http://www.java-samples.com/images/jsf-lifecycle.gif

Don
  • 124
  • 7
-1

Java Server Faces is an MVC web framework where the MVC components are as follows,

  1. Model - It is the managed bean class annotated with @ManagedBean, which has properties to hold the data and respective getters and setters. The managed bean class can also contain the business logic.These are also known as backing beans which can have different scopes like request, session, application.

  2. View - The user interface shown to the client i.e. .xhtml files. It gets the data from the managed beans and it is rendered as the response.

  3. Controller - javax.servlet.webapp.FacesServlet is the centralized controller class which is basically a servlet. Any request that comes to the JSF first goes to the FacesServlet controller. Unlike the JSP in which we write our own controller class, in JSF the controller servlet is a fixed part of the framework and we do not write it.

MVC flow-

enter image description here

Hetal Rachh
  • 1,393
  • 1
  • 17
  • 23
  • This is identical to the 36 upvoted question. If you think that answer can be made more clear, please edit it – Kukeltje Jan 10 '19 at 16:45
  • @Kukeltje I believe my answer has more explaination rather than just naming the MVC components. – Hetal Rachh Jan 10 '19 at 16:51
  • Very little more explanation... 1: not really since 'containing data' is not the best thing (should be in entities), businesslogic, not the best thing since these should be in services (see the additional links in the most upvoted answer), 2: not much more 3: a little more text but missing the image of the other answer which makes more clear than the text you added... – Kukeltje Jan 10 '19 at 17:09
  • @Kukeltje Thanks but I am just a beginner to JSF. – Hetal Rachh Jan 11 '19 at 06:06
  • No problem. I've been using it for a long time now with great success. Get your hands on the java ee 8 and jsf book by Bauke Scholtz and Arjan Thijms. Great read and tutorial, but also read all > 25 upvoted Q/A in Stackoverflow remember their existence and start with jsf 2.3 and – Kukeltje Jan 11 '19 at 08:35