第四篇:设计模式与Struts介绍篇

严重警告:只关心程序的人请跳过<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

事先声明:本篇全是介绍性的东西,根本没谈到编程。本篇所有内容均不是本人作品。也没有修改的地方,只是删除了些内容。为什么要加这篇内容?看了能明白什么?等等诸类问题,本人概不知道,但要声明:觉得不是废话。

设计模式Design Patten

简单来说,Design Patten 就是一个常用的方案。 在我们的开发过程中,经常会遇到一些相同或者相近的问题,每次我们都会去寻找一个新的解决方法,为了节省时间提高效率,我们提供一些能够解决这些常见问题的,被证实可行的方案,构成一个统一的资源库。

一个Design Patten描述了一个被证实可行的方案。这些方案非常普通,是有完整定义的最常用的模式。 这些模式可以被重用,有良好的伸缩性。

MVC设计模式

代码分离,显示与逻辑解耦

验证处理

流程控制

更新应用程序的状态

MVC 通过将问题分为三个类别来帮助解决单一模块方法所遇到的某些问题,这就

Model(模型) View (视图) Controller(控制器)

Model(模型)

模型包含了应用程序的核心,它封装了应用程序的数据结构和事务逻辑,集中体现了应用程序的状态。有时候它仅包含了状态信息,因为它并不了解视窗或控制器的信息(任何时候它对视图或控制器都一无所知 )。

JavaBean EJB 很适合扮演这个角色,因为它能够处理绝大部分事务逻辑和数据结构。它能够与数据库或文件系统进行交互,承担维护应用程序数据的责任。

View(视图)

视图实现模块的外观, 它是应用程序的外在表现。它可以访问模型的数据, 却不了解模型的情况, 同时它也不了解控制器的情况。当模型发生改变时, 视图会得到通知, 它可以访问模型的数据,但不能改变这些数据 。

Controller(控制器)

控制器对用户的输入做出反应并且将模型和视图联系在一起。servlet能够接受客户端的HTTP请求, 并且根据需要创建所需的JavaBean或者EJB,然后将产生的变化通知给视窗 。

面向WEB应用的 MVC Model 2 模式

Web应用的特点

客户机和服务器的无状态连接

由于HTTP本身缺乏状态信息,客户端必须对服务器进行再查询才能发现由输入所造成的改变,在这种情况下,控制器不能将应用程序的改变通知视图。

实现视图所用的技术与实现模型或控制器的技术不同。

也可以使用Java代码来生成所有的Html,但这样会产生更多问题。

对于 Web应用,需要修改标准的 MVC 形式。下图显示了 MVC Web 改写版,通常也称为 MVC Model 2 MVC 2

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="WIDTH: 441pt; HEIGHT: 270.85pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" type="#_x0000_t75"><font size="2"><imagedata o:title="mvc" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C11%5Cclip_image001.png"></imagedata><?xml:namespace prefix = w ns = "urn:schemas-microsoft-com:office:word" /><wrap type="none"></wrap><anchorlock></anchorlock></font></shape>

Struts - - - MVC 2 的一种实现

它是MVC Model 2 的开放源代码的实现

是一组相互协作的类、servlet JSP 标记,它们组成一个可重用的设计

是一个框架,使用Struts也就决定了你的Application的框架

包含了丰富的标记库和独立于该框架工作的实用程序类,可以当成库使用

Apache小组的一个子项目

Struts 概览

<shape id="_x0000_s1026" style="WIDTH: 459pt; HEIGHT: 167pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" type="#_x0000_t75" alt="Struts 概览"><font size="2"><imagedata src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C11%5Cclip_image003.png" o:href="http://www.huihoo.com/java/struts/mvc2_struts.gif"></imagedata><wrap type="none"></wrap><anchorlock></anchorlock></font></shape>

Client browser(客户浏览器)

来自客户浏览器的每个 HTTP 请求创建一个事件。Web 容器将用一个 HTTP request 作出响应。

Controller(控制器)

控制器接收来自浏览器的请求,并决定将这个请求发往何处。就 Struts 而言,控制器是以 servlet 实现的一个命令设计模式。struts-config.xml 文件配置控制器。

业务逻辑

业务逻辑更新模型的状态,并帮助控制应用程序的流程。就 Struts 而言,这是通过作为实际业务逻辑包装的 Action 类完成的。

Model(模型)的状态

模型表示应用程序的状态。业务对象更新应用程序的状态。ActionForm bean 在会话级或请求级表示模型的状态,而不是在持久级。JSP 文件使用 JSP 标记读取来自 ActionForm bean 的信息。

View(视图)

视图就是一个 JSP 文件。其中没有流程逻辑,没有业务逻辑,也没有模型信息 -- 只有标记。标记是使 Struts 有别于其他框架(如 Velocity)的因素之一。

Struts框架中所使用的组件

<group id="_x0000_s1028" style="WIDTH: 415.3pt; HEIGHT: 207.65pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" coordsize="9183,4599" coordorigin="2355,9348" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shape id="_x0000_s1029" style="LEFT: 2355px; WIDTH: 9183px; POSITION: absolute; TOP: 9348px; HEIGHT: 4599px" o:preferrelative="f" type="#_x0000_t75"><font size="2"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><group id="_x0000_s1030" style="LEFT: 2363px; WIDTH: 9167px; POSITION: absolute; TOP: 9353px; HEIGHT: 4589px" coordsize="3580,2688"><group id="_x0000_s1031" style="WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px" coordsize="1790,384"><rect id="_x0000_s1032" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionServlet</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1033" style="WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1034" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790"><rect id="_x0000_s1035" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">控制器</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1036" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1037" style="WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",384"><rect id="_x0000_s1038" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionClass</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1039" style="WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1040" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,384"><rect id="_x0000_s1041" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">包含事务逻辑</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1042" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1043" style="WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",768"><rect id="_x0000_s1044" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionForm</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1045" style="WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1046" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,768"><rect id="_x0000_s1047" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">显示模块数据</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1048" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1049" style="WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",1152"><rect id="_x0000_s1050" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionMapping</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1051" style="WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1052" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,1152"><rect id="_x0000_s1053" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">帮助控制器将请求映射到操作</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1054" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1055" style="WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",1536"><rect id="_x0000_s1056" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionForward</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1057" style="WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1058" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,1536"><rect id="_x0000_s1059" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">用来指示操作转移的对象</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1060" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1061" style="WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",1920"><rect id="_x0000_s1062" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">ActionError</span></b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1063" style="WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1064" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,1920"><rect id="_x0000_s1065" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">用来存储和回收错误</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1066" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1067" style="WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" coordsize="1790,384" coordorigin=",2304"><rect id="_x0000_s1068" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span lang="EN" style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体">Struts</span></b><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">标记库</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1069" style="WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group><group id="_x0000_s1070" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" coordsize="1790,384" coordorigin="1790,2304"><rect id="_x0000_s1071" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" stroked="f" filled="f" fillcolor="#0c9"><textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 14pt; COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN">可以减轻开发显示层次的工作</span></b><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 14pt; COLOR: black; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 宋体; mso-ansi-language: ZH-CN"><p><font size="2"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1072" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px" filled="f" fillcolor="#0c9" strokeweight="19e-5mm" strokecolor="#a0a0a0"><font size="2"></font></rect></group></group><rect id="_x0000_s1073" style="LEFT: 2355px; WIDTH: 9183px; POSITION: absolute; TOP: 9348px; HEIGHT: 4599px" filled="f" fillcolor="#0c9" strokecolor="#a0a0a0"><font size="2"></font></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

ActionServlet

控制器组件是由org.apache.struts.action.ActionServlet类实现的,这个类是javax.servlet.http.HttpServlet类的扩展,它是这一框架的核心 。

控制器将事件(事件通常是 HTTP post)映射到类的一个 servlet。使用配置文件,不必对这些值进行硬编码。

ActionServlet (Command) 创建并使用 ActionActionForm ActionForward。初始化时读取 struts-config.xml 文件配置该 Command。在创建 Web 项目时,需要扩展 Action ActionForm 来解决特定的问题。

Struts controller的基本功能

1. 截获用户的Http请求

2.把这个请求映射到相应的Action类,如果这是此类收到的第一个请求, 将初始化实例并缓寸

3. 创建或发现一个ActionForm bean实例(看配置文件是否定义),然后将请求过程移植到bean.

4. 调用Action实例的perform()方法并将ActioForm bean,Action Mapping对象,requestresponse对象传给它

5. perform返回一个ActionForword对象,此对象连接到相应的jsp页面

ActionServlet配置

我们需要在web.xml中声明ActionServlet,并且将它配置成启动时进行加载

大多数情况下,标准的servlet就能够满足用户需要

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

Action

所有Action类都扩展org.apache.struts.action.Action类,并且覆盖类中定义的perform()方法。

Action 类是业务逻辑的一个包装 ,用途是将 HttpServletRequest 转换为业务逻辑。

当事件进展到这一步时,输入表单数据(或 HTML 表单数据)已被从请求流中提取出来并转移到 ActionForm 类中。 ActionForm 类被传递到Actino类,这一切都是自动完成的。

扩展 Action 类时要注意简洁。Action 类应该控制应用程序的流程,而不应该控制应用程序的逻辑。通过将业务逻辑放在单独的包或 EJB 中,我们就可以提供更大的灵活性和可重用性。

Action类必须以线程安全的方式进行编程,因为控制器会令多个同时发生的请求共享同一个实例,相应的,在设计Action类时就需要注意以下几点

不能使用实例或静态变量存储特定请求的状态信息,它们会在同一个操作中共享跨越请求的全局资源

如果要访问的资源(如JavaBean和会话变量)在并行访问时需要进行保护,那么访问就要进行同步

Action 的配置

我们需要在struts-config.xml中配置Action的信息

示例如下

<action path="/cautionsave"

type="org.apache.struts.webapp.example.CautionSaveAction"

name="cautionsaveForm"

scope="session"

input="/S63.jsp">

<forward name="select" path="/S64.jsp"/>

</action>

ActionForm

ActionForm类扩展org.apache.struts.action.ActionForm类,程序开发人员创建的bean能够包含额外的属性。

框架假设用户在应用程序中为每个表单都创建了一个ActionForm bean,维护 Web 应用程序的会话状态。

如果使用动态ActionForm类,则只需在struts-config.xml中进行相应的配置,框架可自动生成ActionForm bean

典型的ActionFrom bean只有属性的设置与读取方法(getXXX,而没有实现事务逻辑的方法。只有简单的输入检查逻辑,使用的目的是为了存储用户在相关表单中输入的最新数据,以便可以将同一网页进行再生,同时提供一组错误信息,这样就可以让用户修改不正确的输入数据。

Struts 框架将对ActionForm执行以下操作

检查 UserActionForm 是否存在;如果不存在,它将创建该类的一个实例。

将使用 HttpServletRequest 中相应的域设置 ActionForm 的状态。没有太多讨厌的 request.getParameter() 调用。

Struts 框架在将 ActionForm传递给业务包装 Action 之前将更新它的状态。

在将它传递给 Action 类之前,Struts 还会对 ActionForm调用 validation() 方法进行表单状态验证,但不提倡这种做法。

可在会话级维护 ActionForm

ActionForm 的配置

struts-config.xml 文件控制 HTML 表单请求与 ActionForm 之间的映射关系。

可将多个请求映射到 ActionForm

ActionForm可跨多页进行映射,以执行诸如向导之类的操作。

示例如下(非动态ActionForm的配置):

需要开发org.apache.struts.webapp.example.LogonForm.java文件。

LogonForm.java中只需定义私有的对应页面表单的域及其SetXXX()GetXXX()方法。

<form-bean name="logonForm" type="org.apache.struts.webapp.example.LogonForm">

<form-property name=“userpwd" type="java.lang.String"/>

<form-property name=“username" type="java.lang.String"/>

</form-bean>

示例如下(动态ActionForm的配置):

如果需要利用框架的数据校验功能,则Type=“org.apache.struts.validator.DynaValidatorForm”

否则type=“org.apache.struts.action.DynaActionForm”

<form-bean name="cautionsaveForm" dynamic="true" type="org.apache.struts.validator.DynaValidatorForm">

<form-property name="payerClientNo" type="java.lang.String"/>

<form-property name="payerClientName" type="java.lang.String"/>

<form-property name="accountno" type="java.lang.String"/>

<form-property name="amount" type="java.lang.String"/>

<form-property name="intereststart" type="java.lang.String"/>

</form-bean>

ActionMapping

struts-config.xml 配置信息被转换为一组 ActionMapping,而后者又被放入 ActionMappings 容器中。

ActionMapping对象帮助进行框架内部的流程控制,它们可将特定请求URI映射到特定Action,并且将Action类与ActionForm bean相关联。

ActionServlet (Command) 通过 perform() 方法将 ActionMapping 传递给 Action 类。这样就使 Action 可访问用于控制流程的信息。

Action将使用ActionMappingfindForward()方法,此方法返回一个指定名称的ActionForward,这样Action就完成了本地转发。

ActionError

ActionError Struts 保持错误列表的方式,封装了单个错误消息。

ActionError类从不独立进行错误处理,它们总是被存储在ActionErrors对象中,View 可以使用标记访问这些类 。 如下所示:

<html:errors/>

Struts标记库

JSP视图组件所使用的 struts 标记库由四类标记组成

Bean标记:用来在JSP页中管理bean

逻辑标记:用来在JSP页中控制流程

HTML标记:用来生成HTML标记,在表单中显示数据,使用会话IDURL进行编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值