我想清楚地了解开发 JSF 应用程序的正确方法。
在我们的项目中,模型和控制器是同一个类,由 CDI bean 表示。我有点困惑这是否是一个好的做法。据我了解 MVC 模式,控制器应该处理用户操作(例如提交表单),模型应该包含业务逻辑和数据。或者他们在同一个班级可以吗?
另外,每个页面都应该有自己单独的控制器/模型(即这被认为是一个好的实践)吗?
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)?
发布评论
评论(2)
一般来说,最好有两层 - 一层包含 JSF 托管 Bean(可能由 CDI 管理),另一层包含与使用它们的 Web 框架无关的 Bean。
至于“控制器” -
FacesServlet
可以被视为“前端控制器” 对于整个应用程序。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 控制器只是 UI 处理程序,它们不应该携带任何业务逻辑。通常还有更多的层(我在这里只包括与逻辑相关的层):
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):