📄 introduction.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>The Struts User's Guide - Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="Craig R. McClanahan" name="author" />
<meta content="Mike Schachter" name="author" />
<meta content="Larry McCay" name="author" />
<meta content="Ted Husted" name="author" />
<meta content="Martin Cooper" name="author" />
<meta content="Ed Burns" name="author" />
<meta content="Dominique Plante" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">
<p>User Guide</p>
<ul>
<li>
<a href="index.html">Table of Contents</a>
</li>
<li>
<a href="preface.html">Preface</a>
</li>
<li>
<a href="introduction.html">Introduction</a>
</li>
<li>
<a href="building_model.html">Model Components</a>
</li>
<li>
<a href="building_view.html">View Components</a>
</li>
<li>
<a href="building_controller.html">Controller Components</a>
</li>
<li>
<a href="configuration.html">Configuration</a>
</li>
<li>
<a href="release-notes.html">Release Notes</a>
</li>
<li>
<a href="installation.html">Installation</a>
</li>
</ul>
<p>Developer Guides</p>
<ul>
<li>
<a href="dev_bean.html">Bean Tags</a>
</li>
<li>
<a href="dev_html.html">HTML Tags</a>
</li>
<li>
<a href="dev_logic.html">Logic Tags</a>
</li>
<li>
<a href="dev_nested.html">Nested Tags</a>
</li>
<li>
<a href="dev_tiles.html">Tiles Tags</a>
</li>
<li>
<a href="dev_util.html">Utilities</a>
</li>
<li>
<a href="dev_validator.html">Validator</a>
</li>
</ul>
<p>Quick Links</p>
<ul>
<li>
<a href="../index.html">Welcome</a>
</li>
<li>
<a href="index.html">User and Developer Guides *</a>
</li>
<li>
<a href="../faqs/index.html">FAQs and HowTos</a>
</li>
</ul>
<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>Craig R. McClanahan</li>
<li>Mike Schachter</li>
<li>Larry McCay</li>
<li>Ted Husted</li>
<li>Martin Cooper</li>
<li>Ed Burns</li>
<li>Dominique Plante</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1 id="introduction">1. Introduction</h1>
<h2 id="history">1.1 Forward into the Past! (or a brief history of Struts)</h2>
<div class="indent">
<p>
When Java servlets were first invented, many programmers quickly realized that they were a
Good Thing. They were faster and more powerful that standard CGI, portable, and infinitely
extensible.
</p>
<p>
But writing HTML to send to the browser in endless <code>println()</code> statements was tiresome and
problematic. The answer to that was
<a href="http://java.sun.com/products/jsp/product.html">JavaServer Pages</a>,
which turned
<a href="http://java.sun.com/products/jsp/product.html">Servlet</a> writing inside-out.
Now developers could easily mix HTML with Java code, and have all the advantages of servlets.
The sky was the limit!
</p>
<p>
Java web applications quickly became "JSP-centric". This in-and-of itself was not a Bad
Thing, but it did little to resolve flow control issues and other problems endemic to web
applications.
</p>
<p>Another model was clearly needed ...</p>
<p>
Many clever developers realized that JavaServer Pages AND servlets could be used <strong>
together</strong> to deploy web applications. The servlets could help with the control-flow, and the
JSPs could focus on the nasty business of writing HTML. In due course, using JSPs and servlets
together became known as
<a href="http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html">Model 2</a>
(meaning, presumably, that using JSPs alone was Model 1).
</p>
<p>
Of course, there is nothing new under the Sun ... and many have been quick to point out that
JSP's Model 2 follows the classic
<a href="http://java.sun.com/blueprints/patterns/j2ee_patterns/model_view_controller/">Model-View-Controller</a>
design pattern abstracted from the venerable
<a href="http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html">Smalltalk MVC framework</a>.
Java Web developers now tend to use the terms Model 2 and MVC interchangeably. In this guide, we use
the MVC paradigm to describe the Struts architecture, which might be best termed a Model 2/MVC
design.
</p>
<p>
The Struts project was launched in May 2000 by Craig R. McClanahan to provide a standard MVC
framework to the Java community. In July 2001, Struts 1.0 was released, and IOHO, Java
Model 2 development will never be quite the same.
</p>
</div>
<h2 id="mvc">1.2 The Model-View-Controller ('MVC') Design Pattern</h2>
<div class="indent">
<p>
In the MVC design pattern, application flow is mediated by a
central Controller. The Controller delegates requests - in our
case, HTTP requests - to an appropriate handler. The handlers
are tied to a Model, and each handler acts as an adapter
between the request and the Model. The Model represents, or
encapsulates, an application's business logic or
state. Control is usually then forwarded back through the
Controller to the appropriate View. The forwarding can be
determined by consulting a set of mappings, usually loaded
from a database or configuration file. This provides a loose
coupling between the View and Model, which can make
applications significantly easier to create and maintain.
</p>
</div>
<h2 id="modelConcepts">1.2.1 The Model: System State and Business Logic JavaBeans</h2>
<div class="indent">
<p>
The <em>Model</em> portion of an MVC-based system can be often be divided into
two major subsystems -- the <strong>internal state</strong> of the system
and the <strong>actions</strong> that can be taken to change that state.
</p>
<p>In grammatical terms, we might think about
state information as <strong>nouns</strong> (things) and actions as <strong>verbs</strong>
(changes to the state of those things).
</p>
<p>
Many applications represent the internal state of the
system as a set of one or more JavaBeans. The bean properties represent
the details of the system' state. Depending on your application's complexity,
these beans may be self contained (and know how to persist their own state),
or they may be facades that know how to retrieve the system's state from another
component. This component may be a database, a search engine, an Entity Enterprise
JavaBean, a LDAP server, or something else entirely.
</p>
<p>
Large-scale applications will often represent the set of possible
business operations as methods that can be called on the bean or beans
maintaining the state information. For example, you might have a shopping
cart bean, stored in session scope for each current user, with properties
that represent the current set of items that the user has decided to
purchase. This bean might also have a <code>checkOut()</code> method
that authorizes the user's credit card and sends the order to the
warehouse to be picked and shipped. Other systems will represent the
available operations separately, perhaps as Session Enterprise JavaBeans
(Session EJBs).
</p>
<p>
In a smaller scale application, on the other hand, the available
operations might be embedded within the <code>Action</code> classes that are
part of the Struts control layer. This can be useful when the logic is very
simple or where reuse of the business logic in other environments is not
contemplated.
</p>
<p>
The Struts framework architecture is flexible enough to support most any
approach to accessing the Model, but we <strong>strongly</strong> recommend that you
separate the business logic ("how it's done") from the role that
<code>Action</code> classes play ("what to do"). 'nuff said.
</p>
<p>
For more about adapting your application's Model to Struts, see the
<a href="building_model.html">Building Model Components</a> chapter.
</p>
</div>
<h2 id="presentationConcepts">1.2.2 The View: JSP Pages and Presentation Components</h2>
<div class="indent">
<p>
The <em>View</em> portion of a Struts-based application is most often
constructed using JavaServer Pages (JSP) technology. JSP pages can
contain static HTML (or XML) text called "template text", plus the
ability to insert dynamic content based on the interpretation (at page
request time) of special action tags. The JSP environment includes a
set of standard action tags, such as <code><jsp:useBean></code>
whose purpose is described in the <a href="http://java.sun.com/products/jsp/download.html">JavaServer Pages Specification</a>.
In addition to the built-in actions, there is a standard facility to
define your own tags, which are organized into "custom tag libraries."
</p>
<p>
Struts includes a set of custom tag libraries that facilitate
creating user interfaces that are fully internationalized and
interact gracefully with <code>ActionForm</code> beans.
ActionForms capture and validate whatever input is required by
the application.
</p>
<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -