`
xiaotao.2010
  • 浏览: 211979 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Struts-笔记-5

阅读更多

6.Struts MVC 组件

6.1 组件

ActionServlet Action Classes Action Mapping (包含 ActionForward ), ActionForm Bean

6.2 Struts 中的 MVC

1 、模型( Model ):本质上来说在 Struts Model 是一个商业逻辑类,开发者实现商业逻辑。

2 、视图( View ), View 是由与控制器 Servlet 配合工作的一个整套 JSP 定制标签库构成,利用他们可以快速建立应用系统的界面。

3 、控制器( Control ),前端控制器(中心控制器)是一个 Servlet ,他将客户端请求转发到相应的后端控制器 Action

6.3 ActionServlet( 中型控制器 )

定义:

继承自 javax.servlet.http.HttpServlet 类,是中心控制器(总控制器)。它提供一个中心位置来处理全部的终端请求。

 

作用:

       接受请求、填充数据、派发请求、相应用户

配置:

       Web 配置文件( web.xml )声明。

       例:

              <servlet>

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

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

       </servlet>

 

       <servlet- mapping >

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

              <url-pattern>*.do</url-pattern>

       </servlet-mapping>

 

Struts1.3_API ActionServlet 配置文件的说明

 

 

6.4 Action 原理(后端控制器 Action 类)

 

 

       后端控制器 Action 类:

       问题:

1、   Action 什么时候初始化?

发出该 Action 请求,不是在读取配置时初始化。

2、   Action 会初始化几次?

每个 Action 指挥初始化一次。

3、   Struts _1 中的 Action 是线程不安全的,因为所有请求共享一个 Action 实例。

编程人员需要将它变得安全( Struts_2 中的 Action 是安全的)。

Struts_1  API_Action

        

4、   怎样实现 Action 的安全性编程。

n            注意不要用实例变量或者类变量共享只是针对某个请求的数据

n            注意资源操作的同步性。

应用( Action 不安全性的):

        统计一个 Action 访问次数。(创建同步性)

          

                     代码:

                 

6.5 Action Mapping

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

包括:

<action       path=””         type=””          name=””>

           <forward        name=””                path=””>   </forward>

</action>

 

代码实例:

< action-mappings >

     < action path = "/login" type = "com.ibm.LoginAction" name = "loginForm" >

         < forward name = "loginSuccess" path = "/LoginSuccess.jsp" ></ forward >

         < forward name = "loginFailure" path = "/LoginFailure.jsp" ></ forward >

     </ action >

</ action-mappings >

 

              使用 mapping 中的方法:

                                    

                     注意:

                            Mapping 类中的 只能使用使用 mapping.getX(); 方法。

Mapping.setX() ;方法被冻结 不能由用户使用。若用户强行使用抛出

异常。(此处体现出软件设计思想)

 

6.6 ActionForward (导航器)

       实例:

                             

       ActionForward 对象是配置对象。这些配置对象拥有独一无二的标识以允许他们按照 name 属性等来检索。

       ActionForward 对象封装了向前进的 URL 路径且被请求处理器用于识别目标视图。

      

       属性 :

              Name 逻辑名称。

              Path     页面或者模块访问路径。

              Redirect

                            False no ): ------ResquestDispacher.forwa () ;容器内跳转   

                                                        Path= “相对路径”;

                            True yes ): -----HttpServletResponse.sendRedirect(); 容器外跳转

                                                        Path= “绝对路径”;例: path=”http://www.baidu.com”;

 

 

6.7 ActionForm

       工作原理

       处理 ActionForm 的一般步骤:

1、   检查 Action 的映射,确定 Action 中已经配置了对 ActionForm 的映射。

2、   根据 name 属性,查找 form bean 的配置信息。

3、   检查 Action form bean 的使用范围,确定在此范围下 (request,sesstion), 是否已经有次 form bean 的实例。(作用范围: scope

4、   假如当前范围下,已经存在了此 form bean 的实例,而是对当前请求来说,是同一种类型的话,那么就重用。

5、   否则(当前范围下,没有此 form bean 的实例),就重新构建一个 form bean 的实例(调用构造方法),并且保存在一定作用范围。

6、   Form bean reset() 方法被调用

7、   调用相应的 setter 方法,对状态属性赋值

8、   如果 validate 的属性设置为 true ,那么就调用 form bean validate() 方法。( validate :校验)

9、   如果 validate() 方法没有返回任何错误,控制器讲 ActionForm 作为参数,传给 Action 实例的 execute() 方法并执行。

注: reset ()和 validate () 方法是直接继承于 ActionForm 类,并不能实现什么处理能力,所以必须重写覆盖。

 

 

调用构造方法

(后有分析)

                    

 

Input 所指路径

 

 

 

                                   注意:

1、   无参构造方法必须有

2、   Scope 缺省值是 session

 

 

 

分析:

       Scope.setAttribute ();

3、   从过程去查看

HttpSessionAttributeListener

ServletRequestAttributeListener

                                          4 、从结果去查看

 

 

                     备注:     1 、(在 Action 拿到 Form 的值)

             

// 测试实例化一个 FORM 并加入 Scope.setAttribute name form

      

       // 等到这个 AddStudnetForm 的实例

       AddStudentForm addStudentFormInScope = null ;

      

       // 判断传递的作用范围 (request||session)

       if (mapping.getScope().equals( "request" )){

           // 得到从 request 作用范围类传递的 AddStudentForm 的实例;

           addStudentFormInScope = (AddStudentForm)request.getAttribute( "addStudentForm" );

       } else {

           // 得到从 session 作用范围内传递的 AddStudentForm 的实例;

           addStudentFormInScope = (AddStudentForm)request.getSession().getAttribute( "addStudentForm" );

       }

       System. out .println(addStudentFormInScope.getMajor());

      

       System. out .println(addStudentFormInScope.getScore());

      

       System. out .println(addStudentFormInScope.getSname());

      

       // 参数 Form 和中心控制器放入的 Form 是相同的

       if (addStudentFormInScope == form){

           System. out .println( "addStudentFormInScope == form" );

          

       }

             

                     2 JSP 页面提交表单传入参数时, Form 实例化时调用 JSP 表单参数对应的标准的 set 方法。

                            比如: JSP  页面提交参数(标签的 name=’’ 属性)是 username;

                                     Form  则去调用 相对应的 setUsername(); 方法。而不是 Form username 属性。

 

                     3 validate ActionMapping mapping,javax.servlet.http.HttpServletRequest request

                            Org.apache.struts.action.ActionForm

                            Return super.validate(mapping,request);

                            注:此处的返回值 super.validate(mapping , request) = null;

                            Validate 属于 <action ></action> 属性

                            例:

                    

< action path = "/addStudentAction" type = "com.ibm.AddStudentAction"

           name = "addStudentForm"   validate = "true" >

                            若不写,则默认为 validate = “true”;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics