There are a couple of approaches to MVC in Java EE.
The somewhat older approach (but depending on the context still valid) uses JSP for the view and Servlets for the controller. It's often debated what the model exactly is, but it's typically taken to be Services (represented by e.g. EJB session beans) that return domain entities (represented by e.g. JPA Entities).
In modern versions of Java EE, there's a default MVC framework called JSF. Following this framework, Facelets is used for the view and the controller is given (you don't need to implement it).
There's an in-between concept called the backing bean, which is often referred to as the model, but isn't a pure model itself. Instead, it delegates to the real model (e.g. EJB services). The backing bean can also take up some controller responsibilities (issuing a redirect, or putting a message in a kind of queue for the view to display).
Sometimes it's thought that creating a web and business tier is overkill, but this absolutely doesn't have to be the case. It's often just a matter of applying sound OO principles. The other extreme, e.g. stuffing everything on a JSP (html code, controller logic AND business code) is far, far worse.
并且不要使不必要的复杂化。如果您只有一个 Web 用户界面而没有其他用户界面,那么划分为 Web 层和业务层就太过分了。
If you use servlets, it's already Java EE.
And don't make unnecessariliy complex. If you only have a web user interface and no other user interface, dividing into both a web and business tier is over-kill.
发布评论
评论(2)
Java EE 中有多种 MVC 方法。
有点旧的方法(但取决于上下文仍然有效)使用 JSP 作为视图,使用 Servlet 作为控制器。人们经常争论模型究竟是什么,但它通常被认为是返回域实体(例如由 JPA 实体表示)的服务(例如由 EJB 会话 bean 表示)。
在现代版本的 Java EE 中,有一个名为 JSF 的默认 MVC 框架。遵循此框架,Facelets 用于视图并给出控制器(您不需要实现它)。
有一个称为
backing bean
的中间概念,它通常称为模型,但本身并不是纯粹的模型。相反,它委托给真实模型(例如EJB 服务)。支持 bean 还可以承担一些控制器职责(发出重定向,或将消息放入某种队列中以供视图显示)。有时,人们认为创建网络和业务层有些过分,但事实并非如此。这通常只是应用合理的面向对象原则的问题。另一个极端,例如将所有内容都塞在 JSP 上(html 代码、控制器逻辑和业务代码)则要糟糕得多。
请参阅以下示例,了解 3 层(实际上是 3 层)Java EE MVC 应用程序有多么简单: 最小的 3 层 Java EE 应用程序,没有任何 XML 配置
一个相关问题是:主要有哪些MVC 模式相对于老式 3 层模式的优点
There are a couple of approaches to MVC in Java EE.
The somewhat older approach (but depending on the context still valid) uses JSP for the view and Servlets for the controller. It's often debated what the model exactly is, but it's typically taken to be Services (represented by e.g. EJB session beans) that return domain entities (represented by e.g. JPA Entities).
In modern versions of Java EE, there's a default MVC framework called JSF. Following this framework, Facelets is used for the view and the controller is given (you don't need to implement it).
There's an in-between concept called the
backing bean
, which is often referred to as the model, but isn't a pure model itself. Instead, it delegates to the real model (e.g. EJB services). The backing bean can also take up some controller responsibilities (issuing a redirect, or putting a message in a kind of queue for the view to display).Sometimes it's thought that creating a web and business tier is overkill, but this absolutely doesn't have to be the case. It's often just a matter of applying sound OO principles. The other extreme, e.g. stuffing everything on a JSP (html code, controller logic AND business code) is far, far worse.
See this example of how simple a 3 tier (3 layer actually) Java EE MVC application can be: Minimal 3-tier Java EE app, without any XML config
A related question is this one: What are the main advantages of MVC pattern over the old fashioned 3-layer pattern
如果您使用 servlet,那么它已经是 Java EE。
并且不要使不必要的复杂化。如果您只有一个 Web 用户界面而没有其他用户界面,那么划分为 Web 层和业务层就太过分了。
If you use servlets, it's already Java EE.
And don't make unnecessariliy complex. If you only have a web user interface and no other user interface, dividing into both a web and business tier is over-kill.