Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding JSF as a MVC framework

I am reading on JSF and I feel rather confused why JSF is a MVC framework (or atleast which parts belongs to which "letter").

I looked at this question: What components are MVC in JSF MVC framework?

I read there if you don't look at it in an aggregated view the model is your entity, view is your XHTML code and controller is the managed bean. Hmm...Ok, but doesn't the view very often depend on carrying out further business logic calls which returns a set of entities for example, does the description still fit?

One book I read described it as managed beans is the some kind of "message" bringer that the Faces Servlet (Controller) use to invoke the business layer (Model) and then the XHTML code is the view.

There are so many explanations and differences so I don't know which or how to understand it.

like image 762
LuckyLuke Avatar asked Apr 11 '12 17:04

LuckyLuke


People also ask

Is JSF a MVC framework?

The JSF framework implements the Model-View-Controller (MVC) architecture ensuring that applications are well designed and easier to maintain.. Encapsulates the information (data) and the methods to operate on that information (business logic). Managed beans define the model of a JSF application.

How does JSF framework work?

JSF technology is a framework for developing, building server-side User Interface Components and using them in a web application. JSF technology is based on the Model View Controller (MVC) architecture for separating logic from presentation.

Is JSF a web framework?

JSF stands for JavaServer Faces. It is a framework that helps you build user interfaces for JavaServer applications. It provides the standard set of tools for creating a user interface. JavaServer Faces (JSF) is an MVC web framework.


2 Answers

Part of the reason why it's often not entirely clear in JSF and many other web frameworks which parts of it correspond to which part of MVC, is that the MVC pattern was originally devised for desktop applications.

In a desktop application, the nodes M, V and C are a maximum connected graph, meaning each part can communicate with every other part. E.g. if the model changes, it can push this change to the view. This is particularly visible in case there are multiple representations of the view in a desktop application. Change one, and see the other update in real-time.

Due to the client/server and request/response nature of web applications, classic MVC doesn't map 1:1 to most web frameworks.

Specifically, in JSF the mapping is as follows:

  • Model - The Services/DAOs plus the entities they produce and consume. The entry point to this is the managed bean, but in Java EE (of which JSF is a part) these artifacts are typically implemented by EJB and JPA respectively.
  • View - The UI components and their composition into a full page. This is fully in the domain of JSF and implemented by JSF UIComponents and Facelets respectively.
  • Controller - The traffic cop that handles commands and incoming data from the user, routes this to the right parts and selects a view for display. In JSF one doesn't write this controller, but it's already provided by the framework (it's the FacesServlet).

Especially the last part is frequently not well understood: In JSF you don't implement a controller. Consequently, a backing bean or any other kind of managed bean is NOT the controller.

The first part (the model) is also not always clearly understood. Business logic may be implemented by EJB and JPA, but from the point of view of JSF everything that is referenced by a value binding is the model. This is also where the name of one of the JSF life-cycle phases comes from: Update Model. In this phase JSF pushes data from the UI components into the model. In that sense, (JSF) managed beans are thus the model.

Although JSF itself doesn't explicitly define the concept, there is an often recurring and specific usage of managed beans called the backing bean.

For JSF a backing bean is still the model, but practically it's a plumbing element that sits in the middle of the Model, View and Controller. Because it performs some tasks that may be seen as some controller tasks, this is often mistaken to be the controller. But, as explained before this is not correct. It can also perform some model tasks and occasionally do some view logic as well.

See also:

  • What are the main advantages of MVC pattern over the old fashioned 3-layer pattern
  • MVC-Architecture of JavaServer Faces (Chapter 4.3)
like image 89
Arjan Tijms Avatar answered Sep 19 '22 22:09

Arjan Tijms


In a minimalist form, it's:

  • Model: Anything you use for persistence (Hibernate, JPA, etc) and data modeling (Java Beans).
  • View: xhtml, jsp, etc.
  • Controller: Mananaged Beans.

JSF gives you the power to control your requests/responses. The way you create model/view it's not directly connected to the framework MVC concept. It's just a matter of choice. The MVC concept is related to code organization.

Analogously Struts is a MVC framework, but it works mainly as a controller.

I think I help you to better clarify your idea.

like image 39
axcdnt Avatar answered Sep 21 '22 22:09

axcdnt