📄 lib0035.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Presentation Layer</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0034.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0036.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch05"></a>
<div class="section">
<h2 class="first-section-title"><a name="172"></a><a name="ch05lev1sec6"></a>Presentation Layer</h2><p class="first-para">The presentation layer is the section of the application responsible for everything end users physically see in the user interface. Various deployment wrappers provide functionality to the presentation layer.</p>
<p class="para">J2EE developments support HTML/Javascript interfaces and applet interfaces. Most applications provide HTML. J2EE applications produce HTML interfaces by using a combination of static HTML pages and dynamically generated content via servlets and JSPs. There are several good texts for servlets and JSPs (e.g., see <a href="LiB0037.html#181" target="_parent" class="chapterjump">Hall, 2000</a>; <a href="LiB0037.html#182" target="_parent" class="chapterjump">Hunter and Crawford, 2001</a>). Applets can be used for applications that require advanced controls (actions on mouse-overs, drag-drops, sophisticated interactive display controls, etc.).</p>
<p class="para">The presentation layer uses deployment wrappers exclusively. The reason is that the presentation might not execute on the same host as the business logic and will require distributed services. Although it is technically possible to run the servlet engine on the same host as the J2EE container, some organizations prefer not to.</p>
<p class="para">Most organizations use a variant of the model-view-controller (MVC) <a name="173"></a><a name="IDX-67"></a>pattern for the presentation layer. The MVC pattern is a natural choice for Java because Swing uses the MVC pattern as well. The MVC pattern consists of three parts: (1) the <b class="bold">model</b> tracks session state and relevant session information, (2) the <b class="bold">controller</b> interprets all URLs and directs changes to the appropriate model if necessary, and (3) the <b class="bold">view</b> presents information in the model. In a J2EE world, typically the model is a combination of deployment wrappers (enterprise beans, Web services, and RMI services), the controller is a servlet, and the view is a JSP.</p>
<p class="para">The most popular implementation of the MVC pattern designed specifically for J2EE platform user interfaces is Struts. An open source product from the Apache Jakarta project (<a target="_top" class="url" href="http://jakarta.apache.org/struts/">http://jakarta.apache.org/struts/</a>), Struts provides a generic, configurable servlet controller that supports J2EE viewers and models. <a class="internaljump" href="#ch05fig10">Figure 5.10</a> is a diagram of the basic parts of Struts and how it fits the MVC pattern.</p>
<div class="figure">
<a name="174"></a><a name="ch05fig10"></a><span class="figuremediaobject"><a href="images/fig85%5F01%5F0%2Ejpg" NAME="IMG_11" target="_parent"><img src="images/fig85_01.jpg" height="199" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 5.10: </span>MVC Pattern as It Appears in Struts</span>
</div>
<p class="para">The classes depicted in the figure (plus an XML configuration file) are the basics needed to implement Struts. Although Struts has many more classes, a major focus of this book is to shorten the learning curve for new architects, so I stick to the basics. Readers desiring a more in-depth knowledge of Struts should see <a href="LiB0037.html#183" target="_parent" class="chapterjump">Spielman (2003)</a>.</p>
<a name="175"></a><a name="IDX-68"></a>
<p class="para">In a Struts world, there are several components that the developer provides. First, a developer-provided XML configuration file (struts-config.xml) tells the <span class="fixed">ActionServlet</span> which <span class="fixed">Action</span> to invoke and which JSP to forward to based on parameters in the URL. The <span class="fixed">ActionServlet</span> class from Struts is serving as the controller for the MVC pattern, and JSPs serve as a view. You will read more about the configuration format and features available for Struts in <a href="LiB0094.html#450" target="_parent" class="chapterjump">chapter 14</a>.</p>
<p class="para">Second, the developer optionally provides extensions of <span class="fixed">ActionForm</span> to validate user input. If errors are found, Struts navigates the user back to the URL specified in the configuration file. Your JSPs are responsible for figuring out if there are errors to display and physically displaying error messages.</p>
<p class="para">By the way, the Struts <span class="fixed">ActionForm</span> is also intended as a mechanism to collect user-entered data from a Web page. Use of this feature is optional. If you do use it, any page that has user entry will require an <span class="fixed">ActionForm</span>. This is a Struts feature I rarely use.</p>
<p class="para">Third, developer-provided extensions of <span class="fixed">Action</span> can call objects in the deployment layer, such as enterprise beans, Web services, RMI services, and so on. Calls to the deployment layer initiate some type of process within your application or retrieve data to be displayed to the user. The displayed data are put on the session (just as in a custom servlet). Your JSPs will retrieve any information needed for display from the session.</p>
<p class="para">Fourth, the developer provides JSPs and/or static HTML pages to display to the user. The <span class="fixed">ActionServlet</span> forwards the request after the <span class="fixed">Action</span> has completed. Don't worry, Struts does provide a way to dynamically change the forward.</p>
<p class="last-para">Struts has the advantage of giving you controller functionality you would otherwise have to build, and it's popular, which means many developers already know it. Its chief disadvantage is that it's complex and not the easiest creature to debug when problems arise. Despite its complexity, I usually opt for using it. I'll provide a cheat sheet for getting up to speed on Struts in <a href="LiB0094.html#450" target="_parent" class="chapterjump">chapter 14</a>.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0034.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0036.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -