小黑struts学习(五) Action Mapping、ActionForward和ActionForm组件学习

小黑struts学习(五) Action Mapping、ActionForward和ActionForm组件学习
接着学习struts的相关组件
Action Mapping
 每一个<action>元素都与类org.apache.struts.action.ActionMapping的一个实例对应,包括name,path,forward。

An ActionMapping represents the information that the controller, RequestProcessor, knows about the mapping of a particular request to an instance of a particular Action class. The ActionMapping instance used to select a particular Action is passed on to that Action, thereby providing access to any custom configuration information included with the ActionMapping object.

下面利用Action Mapping的相关方法 测试得到Action Mapping的属性值
在 AddStudentAction增加以下代码
  // test the ActionMapping
        String name  =  mapping.getName();
        String path 
=  mapping.getPath();
        String type 
=  mapping.getType();
        System.out.println(
" name= " + name + " \tpath= " + path + " \ttype= " + type);
        String[] forwardsNames 
=  mapping.findForwards();
        
for (String forwardName:forwardsNames) {
            ActionForward forward 
= mapping.findForward(forwardName);
            String forwardPath 
= forward.getPath();
            System.out.println(
"forwardname="+forwardName+"\tforwardPath"+forwardPath);
        }

        

重新部署web应用 观察控制台输出

其与struts-config.xml中的代码对应
< action-mappings >
          
< action  path ="/addStudentAction"  type ="cn.itcast.AddStudentAction"  name ="addStudentForm" >
         
< forward  name ="addStudentSuccess"  path ="/AddStudentSuccess.jsp" ></ forward >
         
< forward  name ="addStudentFailure"  path ="/AddStudent.jsp" ></ forward >
     
</ action >
     
     
  
</ action-mappings >
注意:外部不可以使用set方法不能设置其属性  因为配置文件已经被frozen!

ActionForward(导航器)
       ActionForward对象是配置对象。这些配置对象拥有独一无二的标识以允许它们按照name属性等来检索。ActionForward对象封装了向前进的URL路径且被请求处理器用于识别目标视觉。
       name:逻辑名称
       path:页面或者模块访问路径

An ActionForward represents a destination to which the controller, RequestProcessor, might be directed to perform a RequestDispatcher.forward or HttpServletResponse.sendRedirect to, as a result of processing activities of an Action class. Instances of this class may be created dynamically as necessary, or configured in association with an ActionMapping instance for named lookup of potentially multiple destinations for a particular mapping instance.

An ActionForward has the following minimal set of properties. Additional properties can be provided as needed by subclassses.

  • contextRelative - Should the path value be interpreted as context-relative (instead of module-relative, if it starts with a '/' character? [false]
  • name - Logical name by which this instance may be looked up in relationship to a particular ActionMapping.
  • path - Module-relative or context-relative URI to which control should be forwarded, or an absolute or relative URI to which control should be redirected.
  • redirect - Set to true if the controller servlet should call HttpServletResponse.sendRedirect() on the associated path; otherwise false. [false]
redirec:
    fale,no    ————      RequestDispatcher.forword  路径相对当前应用
    true,yes  ————      HttpServletResponse.sendRedirec.path 写绝对路径

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



总觉得学习组件有点无聊,也许明天开始学习另外一个实例。其他组件过段时间再学习了!

你可能感兴趣的:(小黑struts学习(五) Action Mapping、ActionForward和ActionForm组件学习)