java - JSF中MVC的矛盾解释

标签 java jsf model-view-controller

我开始学习 JSF,但首先我想了解它作为 MVC 框架的整体情况。

有几个答案和许多赞成票解释了什么是 JSF 中的 MVC 层,但它们通常自相矛盾。

BalusC 的回答: What components are MVC in JSF MVC framework?

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

Jigar Joshi 在同一主题中的回答:

M odel would be your ManagedBean

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

C ontroller will be FacesServlet

Here ,对问题的另一种看法:

In JSF you don't implement a controller. Consequently, a backing bean or any other kind of managed bean is NOT the controller.

Yet another ,这次不是来自 Stackoverflow:

In JSF, the master Controller is always the FacesServlet. Sub-Controllers are incorporated into the various control element tag implementations. You almost never write controller code in JSF, because it's all pre-supplied. So you only have to supply the View templates (xhtml) and the Models (backing beans).

A lot of people think that the action logic in backing beans makes them Controllers. This is incorrect. A Controller is a component whose sole purpose in life is to synchronize the Model and View. In JSF, that task is performed by the FacesServlet and the controls. You may have Validators and Converters performing adjunct functions, but the actual synchronization (updating) is part of the JSF core.

我知道 MVC 有很多变体,具体取决于它是桌面应用程序、Web 应用程序等。因此很难定义 MVC(尝试找到两个对 MVC 有相同解释的来源)。

我主要关心这里的托管 bean。他们是M还是C?托管 bean 显然用于从模型层(最高抽象级别上的模型层 - BalusC 的答案中的大架构图,即 EJB、JPA 和 DAO)检索数据并存储 View 要使用的结果。 MVC 中的 Controller 层负责处理来自 View 的命令、与模型层通信以及从模型层检索数据。托管bean用于与模型层通信吗?是的,它还使检索到的数据可用于 View 。对我来说它属于 Controller 层,而不是模型,因为它不包含用于检索数据或数据本身的逻辑,而只调用适当的模型层方法(看看 BalusC's code sample )。

那么我困惑的根源是什么?任何人都可以一劳永逸地解释这一点,以便 JSF 的初学者清楚吗?

最佳答案

I'm mostly concerned about Managed beans here. Are they M or C?

当他们看起来像这样时,人们会认为他们是 M:

@ManagedBean
public class Bean {

    private String username; // +getter+setter
    private String password; // +getter+setter

    @Resource
    private DataSource dataSource;

    public void login() {
        try (
            Connection connection = dataSource.getConnection();
            PreparedStatement statement = connection.prepareStatement("SELECT * FROM User WHERE username = ? AND password = MD5(?)");
        ) {
            statement.setString(1, username);
            statement.setString(2, password);

            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    // Login.
                }
            }
        }
    }

    // ...
}

但是当他们看起来像这样时,人们会认为他们是 C:

@ManagedBean
public class Bean {

    private User user // +getter

    @EJB
    private UserService userService;

    public void login() {
        if (userService.find(user) != null) {
            // Login.
        }
    }

    // ...
}

在您找到的 MVC 答案中也提到了这一点:

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

另见:

关于java - JSF中MVC的矛盾解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757126/

相关文章:

php - 您应该在 Controller 中还是在 MVC 框架的模型中处理 session 数据?

java - InfiniteProgressDemo 的问题

css - 删除特定数据表上的所有边框

model-view-controller - Joomla组件: one view calling multiple models?

java - JSF2 : Submit AJAX form

jsf - 如何启用/禁用 JSF 命令按钮

php - 如何将从其他函数返回的变量与 mysql 中的匹配行进行比较

java - jPanel 的背景图像不起作用

java - ntlm认证失败后IE7表单认证

java - Java 8 中的可编辑 JComboBox 不会将 Enter 键转发到默认按钮