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

严重警告:只关心程序的人请跳过<o:p></o:p>

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

设计模式Design Patten

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

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

MVC设计模式<o:p></o:p>

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

验证处理

流程控制

更新应用程序的状态

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

Model(模型)  View (视图) Controller(控制器) <o:p></o:p>

Model(模型) <o:p></o:p>

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

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

View(视图) <o:p></o:p>

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

Controller(控制器) <o:p></o:p>

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

面向WEB应用的 MVC Model 2 模式 <o:p></o:p>

Web应用的特点

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

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

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

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

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

<o:p> </o:p>

<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id="_x0000_s1027" type="#_x0000_t75" style="WIDTH: 441pt; HEIGHT: 270.85pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\11\clip_image001.png" o:title="mvc"></v:imagedata><w:wrap type="none"></w:wrap><w:anchorlock></w:anchorlock></v:shape>

Struts - - - MVC 2 的一种实现 <o:p></o:p>

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

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

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

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

Apache小组的一个子项目

Struts 概览 <o:p></o:p>

<o:p> </o:p>

<v:shape id="_x0000_s1026" type="#_x0000_t75" alt="Struts 概览" style="WIDTH: 459pt; HEIGHT: 167pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line"><v:imagedata o:href="http://www.huihoo.com/java/struts/mvc2_struts.gif" src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\11\clip_image003.png"></v:imagedata><w:wrap type="none"></w:wrap><w:anchorlock></w:anchorlock></v:shape>

<o:p> </o:p>

<o:p> </o:p>

Client browser(客户浏览器) <o:p></o:p>

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

Controller(控制器) <o:p></o:p>

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

业务逻辑 <o:p></o:p>

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

Model(模型)的状态<o:p></o:p>

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

View(视图)<o:p></o:p>

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

Struts框架中所使用的组件<o:p></o:p>

<v:group coordsize="9183,4599" id="_x0000_s1028" coordorigin="2355,9348" editas="canvas" style="WIDTH: 415.3pt; HEIGHT: 207.65pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line"><o:lock v:ext="edit" aspectratio="t"></o:lock><v:shape id="_x0000_s1029" type="#_x0000_t75" o:preferrelative="f" style="LEFT: 2355px; WIDTH: 9183px; POSITION: absolute; TOP: 9348px; HEIGHT: 4599px"><v:fill o:detectmouseclick="t"></v:fill><v:path o:extrusionok="t" o:connecttype="none"></v:path><o:lock text="t" v:ext="edit"></o:lock></v:shape><v:group coordsize="3580,2688" id="_x0000_s1030" style="LEFT: 2363px; WIDTH: 9167px; POSITION: absolute; TOP: 9353px; HEIGHT: 4589px"><v:group coordsize="1790,384" id="_x0000_s1031" style="WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1032" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionServlet<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1033" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1034" coordorigin="1790" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1035" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

控制器<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1036" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1037" coordorigin=",384" style="WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1038" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionClass<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1039" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1040" coordorigin="1790,384" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1041" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

包含事务逻辑<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1042" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 384px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1043" coordorigin=",768" style="WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1044" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionForm<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1045" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1046" coordorigin="1790,768" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1047" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

显示模块数据<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1048" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 768px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1049" coordorigin=",1152" style="WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1050" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionMapping<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1051" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1052" coordorigin="1790,1152" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1053" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

帮助控制器将请求映射到操作<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1054" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1152px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1055" coordorigin=",1536" style="WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1056" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionForward<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1057" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1058" coordorigin="1790,1536" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1059" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

用来指示操作转移的对象<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1060" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1536px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1061" coordorigin=",1920" style="WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1062" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

ActionError<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1063" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1064" coordorigin="1790,1920" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1065" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

用来存储和回收错误<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1066" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 1920px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1067" coordorigin=",2304" style="WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1068" style="LEFT: 43px; WIDTH: 1704px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

Struts标记库<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1069" strokeweight="19e-5mm" style="WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"> </v:rect></v:group><v:group coordsize="1790,384" id="_x0000_s1070" coordorigin="1790,2304" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"><v:rect fillcolor="#0c9" filled="f" stroked="f" id="_x0000_s1071" style="LEFT: 1833px; WIDTH: 1704px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"><v:textbox inset="2.00661mm,1.0033mm,2.00661mm,1.0033mm">

可以减轻开发显示层次的工作<o:p></o:p>

<o:p> </o:p>

</v:textbox></v:rect><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1072" strokeweight="19e-5mm" style="LEFT: 1790px; WIDTH: 1790px; POSITION: absolute; TOP: 2304px; HEIGHT: 384px"> </v:rect></v:group></v:group><v:rect strokecolor="#a0a0a0" fillcolor="#0c9" filled="f" id="_x0000_s1073" style="LEFT: 2355px; WIDTH: 9183px; POSITION: absolute; TOP: 9348px; HEIGHT: 4599px"> </v:rect><w:wrap type="none"></w:wrap><w:anchorlock></w:anchorlock></v:group> <o:p></o:p>

ActionServlet <o:p></o:p>

控制器组件是由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的基本功能<o:p></o:p>

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<o:p></o:p>

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

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

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

扩展 Action 类时要注意简

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值