三大框架之Struts1--Struts包含的组件浅析

来源:互联网 发布:音频解码软件下载 编辑:程序博客网 时间:2024/05/04 09:20

Struts包含的组件:
    1.ActionServlet(总控制器) org.apache.struts.action.ActionServlet


    2.Action Classes


    3.Action Mapping(包括ActionForward)


    4.ActionForm Bean


    5.配置文件(协调工作)

 

 

详细介绍:

1.

ActionServlet(中心控制器) RequestProcessor
定义:继承自javax.servlet.http.HttpServlet类,提供一个中心位置来处理全部的终端请求(*.do)。

作用:接受请求、填充数据、派发请求、响应用户

配置:在web.xml中
 <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>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

2.

Action
An Action is an adapte between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request.
a.第一次请求的时候实例化
b.每个Action只会初始化一次
c.Action是线程不安全的,因为所有的请求共享一个action实例
d.怎样实现Action的安全性编程?
@注意不要用实例变量或者类变量共享只是针对某个请求的数据
@注意资源操作的同步性

 

3.

Action Mapping
每一个<action>元素都与类org.apache.struts.action.ActionMapping的一个实例对应。


ActionForward(导航器)
ActionForward对象是配置对象。这些配置对象拥有独一无二的标识以允许它们按照name属性等来检索。ActionForward对象封装了向前进的URL路径且被请求处理器用于识别目标视图。
name:逻辑名称
path:页面或者模块访问路径
redirect:false,no……RequestDispatcher.forward    路径相对当前应用
             true,yes……HttpServletResponse.sendRedirect    path写绝对路径,如Http://www.baidu.com

 

4.

ActionForm
工作原理
处理ActionForm的一般步骤:
(1)检查Action的映射,确定Action中已经配置了对ActionForm的映射
(2)根据name属性,查找form bean的配置信息
(3)检查Action的form bean的使用范围,确定在此范围下scope(request,session),是否已经有此form bean的实例
(4)假如当前范围下,已经存在了此form bean的实例,而且对当前请求来说,是同一种类型的话,那么就重用
(5)否则,就重新构建一个form bean的实例(调用构造方法),并且保存在一定作用范围scope
(6)form bean的reset()方法被调用
(7)调用对应的setter方法,对状态属性赋值
(8)如果validate的属性值为true,那么就调用form bean的validate()方法
(9)如果validate()方法没有返回任何错误,控制器将ActionForm作为参数,传给Action实例的execute()方法并执行