Java MVC Model 2 架构的例子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/970504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Examples of Java MVC Model 2 architecture?
提问by James McMahon
Can anyone post or point me in the direction of a clear example of a from scratchimplementation of Model 2 architecture?
任何人都可以发布或指向我从头开始实现模型 2 架构的清晰示例的方向吗?
Below is a detailed description of Model 2, taken from this page.
以下是来自此页面的 Model 2 的详细说明。
The Model 2 architecture for designing JSP pages is in reality, Model View Controller (MVC) applied to web applications. Hence the two terms can be used interchangeably in the web world. MVC originated in SmallTalk and has since made its way into Java community. Model 2 architecure and its derivatives are the cornerstones for all serious and industrial strength web applications designed in the real world. Hence it is essential for you understand this paradigm thoroughly. Figure 1.2 shows the Model 2 (MVC) architecture.
The main difference between Model 1 and Model 2 is that in Model 2, a controller handles the user request instead of another JSP. The controller is implemented as a Servlet. The following steps are executed when the user submits the request.
- The Controller Servlet handles the user's request. (This means the hyperlink in the JSP should point to the controller servlet).
- The Controller Servlet then instantiates appropriate JavaBeans based on the request parameters (and optionally also based on session attributes).
- The Controller Servlet then by itself or through a controller helper communicates with the middle tier or directly to the database to fetch the required data.
- The Controller sets the resultant JavaBeans (either same or a new one) in one of the following contexts – request, session or application.
- The controller then dispatches the request to the next view based on the request URL.
- The View uses the resultant JavaBeans from Step 4 to display data. Note that there is no presentation logic in the JSP. The sole function of the JSP in Model 2 architecture is to display the data from the JavaBeans set in the request, session or application scopes.
(source: java-samples.com)
用于设计 JSP 页面的 Model 2 架构实际上是应用于 Web 应用程序的模型视图控制器 (MVC)。因此,这两个术语可以在网络世界中互换使用。MVC 起源于 SmallTalk 并从此进入 Java 社区。Model 2 架构及其衍生产品是在现实世界中设计的所有严肃且具有工业强度的 Web 应用程序的基石。因此,您必须彻底了解这个范式。图 1.2 显示了模型 2 (MVC) 架构。
模型 1 和模型 2 之间的主要区别在于,在模型 2 中,控制器处理用户请求而不是另一个 JSP。控制器是作为一个 Servlet 实现的。当用户提交请求时执行以下步骤。
- Controller Servlet 处理用户的请求。(这意味着 JSP 中的超链接应该指向控制器 servlet)。
- 然后,控制器 Servlet 根据请求参数(也可以根据会话属性)实例化适当的 JavaBean。
- 然后,控制器 Servlet 自己或通过控制器助手与中间层通信或直接与数据库通信,以获取所需的数据。
- 控制器在以下上下文之一中设置结果 JavaBeans(相同的或新的) - 请求、会话或应用程序。
- 然后控制器根据请求 URL 将请求分派到下一个视图。
- 视图使用来自步骤 4 的结果 JavaBean 来显示数据。请注意,JSP 中没有表示逻辑。JSP 在 Model 2 体系结构中的唯一功能是显示来自请求、会话或应用程序范围中的 JavaBeans 设置的数据。 (来源:java-samples.com)
采纳答案by Jibba
I like Bear Bibeault'sarticle from a few years ago. High level explanation, but very clear.
我喜欢Bear Bibeault几年前的文章。高水平的解释,但非常清楚。
This is his implementation of an MVC "framework". I learned a lot by going through it:
这是他对MVC“框架”的实现。我通过它学到了很多东西:
回答by Pablojim
Try the Sun Java petstore:
试试 Sun Java petstore:
http://java.sun.com/developer/releases/petstore/
http://java.sun.com/developer/releases/petstore/
Or an older version:
或旧版本:
http://java.sun.com/developer/releases/petstore/petstore1_3_1_02.html
http://java.sun.com/developer/releases/petstore/petstore1_3_1_02.html
回答by Mike Pone
I would download Springand check out some Spring examples too. Also take a look at this tutorial
回答by JeeBee
Maybe Struts 1 (with tiles) would be easier to grok than Spring, if you want to see how MVC works in a Java web application. Ignoring all the Struts tags, you end up with Actions (which are called by the Struts servlet), JSPs and a configuration file or two to tie them together. Lots of information online.
如果您想了解 MVC 在 Java Web 应用程序中是如何工作的,那么 Struts 1(带图块)可能比 Spring 更容易理解。忽略所有 Struts 标记,您最终会得到 Actions(由 Struts servlet 调用)、JSP 和一两个配置文件来将它们联系在一起。网上资料很多。


