没有合适的资源?快使用搜索试试~ 我知道了~
首页Apache Struts MVC Design Pattern
资源详情
资源推荐
Apache Struts MVC Design Pattern
A sample from
Beginning Apache Struts: From Novice to Professional
by Arnold Doray
Servlet technology is the primary base on which you can create web applications with Java. JavaServer Pages
(JSP) technology is based on servlet technology but extends it by allowing HTML content to be created easily.
Note that JSP technology doesn’t replace servlet technology, but rather builds on it, to address some of its
deficiencies. Struts follows this pattern, and builds on servlet and JSP technologies to address their shortcomings. These shortcomings are
in two broad areas:
Creating a “separation of concerns”: Pieces of code that do different things are bundled separately and given standardized
ways of communicating between themselves.
An infrastructure for webapps: This includes things like validating user input, handling and reporting errors, and managing flow
control.
If these shortcomings are addressed, the very practical problems of building webapps (robustness, maintainability, localization, etc.)
become easier to tackle.
One tool that can be used to make some headway in resolving these issues is an organizing principle
called the Model-View-Controller (MVC) design pattern.
WHAT ARE DESIGN PATTERNS?
Design patterns are essentially recipes for solving generic coding problems. They are not algorithms but rather organizational principles for
software.
The idea of using “design patterns” to solve common software engineering problems first came to prominence with the book Design
Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley Professional, 1995), by, by Erich Gamma, Richard Helm, Ralph
Johnson, and John Vlissides. The authors credit an earlier work, A Pattern Language for Building and Construction (Oxford University
Press, 1977) by the architect Christopher Alexander as the inspiration for the idea of design patterns for software.
The MVC design pattern, which is the subject of this chapter, was first used in an AI language called Smalltalk in the 1980s to solve a
problem similar to the one we’re discussing here for webapps. A variation on the pure MVC pattern is also used in Swing, Java’s platform-
independent GUI toolkit.
The MVC design pattern calls for a separation of code by their function:
Model: Code to control data access and persistence
View: Code to handle how data is presented to the user
Controller: Code to handle data flow and transformation between Model and View
Figure 5-1 illustrates MVC.
Figure 5-1. The MVC design pattern
In addition to this separation, there are a couple of implicit constraints that the Model, View, and Controller code follow:
The View gets data from the Model only through the Controller.
The Controller communicates with the View and Model through well-defined, preferably standardized ways. For example,
embedding SQL code directly in your Controller code violates this principle of standardized communication. Using Model classes
that expose functions for data access would be an example of standardized communication between the Controller and the Model.
It may not be immediately obvious to you how this solves anything, but I hope you’ll come to appreciate the power of the MVC approach as
you progress. For now, it is sufficient for you to accept that applying the MVC design pattern helps us achieve the larger goal of separation
of concerns, which greatly simplifies the building of web applications.
Our discussion so far has been quite general for a purpose. Any web application framework will use the MVC principle, so it is to your
advantage to understand it apart from Struts’ implementation of it.
Later, we’ll discuss how Struts implements the MVC design pattern. But before we do, I’ll give you an example of how to apply the MVC
design pattern.
The Registration Webapp
Many web applications have a “user registration” facility. I’m sure you’ve seen this before. Some online news sites, for example, have the
annoying requirement that you “subscribe” before you can view their content.
I’ll call this facility the “Registration Webapp.” What I’ll do is specify the requirements of this webapp, and for each requirement, I’ll identify
the types of code (Model, View, or Controller) that might be needed.
Requirement 1
The user is asked to specify a user ID (userid) and a password, as well as a password confirmation, as shown in Figure 5-2.
剩余16页未读,继续阅读
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功