jFrenetic
jFrenetic

Reputation: 5542

Distinguish between Model and Controller in JSF

I want to clearly understand the proper way of developing a JSF application. In our project, the model and controller is the same class, represented by CDI bean. I'm sort of confused whether it's a good practice. As far as I understand MVC pattern, the controller should handle user actions (such as submitting form) and the model should contain business logic and data. Or is it OK, that they are in the same class?
Also, should every page have its own separate Controller/Model (i.e is it considered a good practice)?

Upvotes: 4

Views: 3077

Answers (2)

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15308

JSF controllers are just UI handlers, they shouldn't carry any business logic. Usually there are even more layers (I include here only layers that relate to logic):

  1. UI Controller - that is tightly tied to the UI and thus to the technology you use for MVC
  2. Service Layer - is a facade-entry point to the business logic, usually also manages transactions and in some cases - DTO/Entity transformation if this is required. It doesn't know anything about Web MVC technology you use.
  3. Business logic that obeys Domain Model pattern (sometimes called Domain Service).

Upvotes: 4

Bozho
Bozho

Reputation: 597124

It is generally a better idea to have two layers - one with JSF managed beans (might be managed by CDI) and another one with beans that are agnostic of the web framework that is using them.

As for the "controller" - the FacesServlet can be viewed as the "front controller" for the entire application.

Upvotes: 6

Related Questions