JSF中MVC的矛盾解释 [英] Contradictory explanations of MVC in JSF

查看:124
本文介绍了JSF中MVC的矛盾解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习JSF,但首先我想了解它作为MVC框架的大局。



许多答案都有很多upvotes解释JSF中的MVC层是什么,但它们通常自相矛盾。



BalusC的回答:
JSF MVC框架中的MVC是什么组件?


在大型建筑图片中,您自己的JSF代码是 V



M - 业务域/服务层(例如EJB / JPA / DAO)

V - 您的JSF代码

C - FacesServlet



在开发者图片中,建筑 V 依次可分割为
,如下所示:



M - 实体

V - Facelets / JSP页面

C - M. anaged bean


Jigar Joshi在同一个帖子中的答案:


M odel将是您的 ManagedBean



V iew将是 jsp XHTML (这里你可以容纳各种观点)



C ontroller将 FacesServlet


这里,另一个关于问题的观点:


在JSF中,您没有实现控制器。因此,支持bean
或任何其他类型的托管bean 控制器。


又一个,而不是来自这次是Stackoverflow:


在JSF中,主控制器始终是FacesServlet。
子控制器被合并到各种控制元素标签
实现中。你几乎从不在JSF中编写控制器代码,
因为它都是预先提供的。因此,您只需提供View
模板(xhtml)和模型(支持bean)。



很多人认为支持bean的动作逻辑会使控制器成为
。这是不正确的。 Controller是
唯一目的的组件,用于同步Model和View。在JSF中,
该任务由FacesServlet和控件执行。您可能
有验证器和转换器执行附加功能,但
实际同步(更新)是JSF核心的一部分。


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



我最关心的是托管bean。他们是M还是C?托管bean显然用于从Model层(最高抽象层上的Model层 - 在BalusC的答案中的大型架构图片,即EJB,JPA和DAO)中检索数据,并存储视图使用的结果。 MVC中的控制器层负责处理来自视图的命令,与模型层通信以及从模型层检索数据。托管bean是否用于与Model层通信?是的,它还使检索到的数据可用于视图。对我来说,它属于控制器层,而不是模型,因为它不包含用于检索数据的逻辑或数据本身,而只调用适当的模型层方法(看看BalusC的代码示例

那么我混淆的原因是什么?任何人都可以一劳永逸地解释这一点,以便JSF的初学者明白这一点吗?

解决方案


<我在这里主要关注托管bean。它们是M还是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()){
//登录。
}
}
}
}

// ... ...
}

但人们认为他们看起来像这样:

  @ManagedBean 
public class Bean {

private User user // + getter

@EJB
private UserService userService;

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

// ... ...
}

你在同一个MVC答案中也提到了这一点:


注意一些初学者甚至一些 - 非常基础 - 教程在托管bean中混合/复制/展平实体的属性,这将有效地使控制器成为模型。不用说,这是糟糕的设计(即不是一个干净的MVC设计)。




参见:




I'd starting to learn JSF, but first I'd like to understand the big picture of it as an MVC framework.

There are several answers with many upvotes explaining what are MVC layers in JSF, but they usually contradict themselves.

BalusC's answer: 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's answer in the same thread:

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, another view on the problem:

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, not from Stackoverflow this time:

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.

I know MVC has many variants depending on if it's a desktop application, web aplication etc. so it's difficult to define MVC (try to find two sources with identical explanation of MVC).

I'm mostly concerned with Managed beans here. Are they M or C? Managed beans are apparently used to retrieve data from Model layer (the Model layer on the highest level of abstraction - big architectural picture as in BalusC's answer, that is EJB, JPA and DAO) and store the result to be used by the view. Controller layer in MVC is the one responsible for handling commands from the view, communicating with model layer and retrieving data from the model layer. Is managed bean used to communicate with Model layer? Yes, and it also makes the retrieved data available for the view. For me it belongs to controller layer, not a model, because it doesn't contain logic used to retrieve the data, or the data itself, but only calls the appropriate model layer methods (take a look at BalusC's code sample).

So what's the source of my confusion? Could anyone explain this once and for all so that it's clear for beginners in JSF?

解决方案

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

People consider them M when they look like this:

@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.
                }
            }
        }
    }

    // ...
}

But people consider them C when they look like this:

@ManagedBean
public class Bean {

    private User user // +getter

    @EJB
    private UserService userService;

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

    // ...
}

This is also mentioned in the very same MVC answer you found:

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

See also:

这篇关于JSF中MVC的矛盾解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆