开发者

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, 开发者_JAVA百科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)?


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.


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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜