org.apache.struts.action.Action类是Struts的心脏,也是客户请求和业务操作间的桥梁。每个Action类通常设计为代替客户完成某种操作。
一旦正确的Action实例确定,就会调用RequestProcessor类的execute()方法。该方法的结构如下:

//摘自org.apache.struts.action.Action类
     public ActionForward execute(ActionMapping mapping, ActionForm form,ServletRequest request,ServletResponse response) throws Exception 
     {
        try
        {
            return execute(mapping, form,(HttpServletRequest) (Object) request,(HttpServletResponse) (Object) response);
        } catch (ClassCastException e) {
            return null;
        }
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
    {
        return null;
    }

在Struts应用程序中,具体的Action子类需要扩展Action类,以提供execute()方法的实现。execute()方法有四个参数:ActionMapping对象,ActionForm对象,HttpServletRequest对象和HttpServletResponse对象。ActionForm对象封装了表单数据,因此Action类可以通过getter方法从该对象中获得表单数据,然后调用模型组件处理这些数据。Action类又通过ActionMapping对象的findForward()方法获得一个ActionForward对象,然后把处理结果转发到ActionForward对象所指的目标。
 Action示例:

package struts.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.LoginHandler;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import struts.form.LoginHandlerForm;

public class LoginHandlerAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
                
        LoginHandlerForm loginHandlerForm = (LoginHandlerForm) form;        
        //从Form中取得表单数据
        String userName = loginHandlerForm.getUserName();
        String userPwd = loginHandlerForm.getUserPwd();
        
        //生成一个Session对象
        HttpSession session = request.getSession(true);
        session.removeAttribute("userName");
        session.setAttribute("userName", userName);
        
        //生成一个ArrayList 
        ArrayList arr = new ArrayList();
        arr.add(userName);
        arr.add(userPwd);
        
        String forward;
        
        //调用模型组件
        LoginHandler login = new LoginHandler();
        boolean flag = login.checkLogin(arr);
        if(flag)
            forward = "success";
        else
            forward = "fail";
        
        //转向
        return mapping.findForward(forward);
        
    }
}

ActionMapping存储了与特定用户请求对应的特定Action的相关信息,例如输入页面,转发页面等。ActionServlet将ActionMapping传送到Action类的execute()方法,然后Action将调用ActionMapping的findForward()方法,此方法返回一个指定名称的ActionForward,这样Action就完成了本地转发。若没有找到具体的ActionForward,就返回一个null。ActionMapping类的源代码如下:

package org.apache.struts.action;
import java.util.ArrayList;

import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ForwardConfig;

public class ActionMapping extends ActionConfig
{
    public ActionForward findForward(String name)
    {
        ForwardConfig config = this.findForwardConfig(name);
        if (config == null)
            config = this.getModuleConfig().findForwardConfig(name);
        return (ActionForward) config;
    }
    
    public String[] findForwards() 
    {
        ArrayList results = new ArrayList();
        ForwardConfig[] fcs = this.findForwardConfigs();
        for (int i = 0; i < fcs.length; i++)
            results.add(fcs[i].getName());
        return (String[]) results.toArray(new String[results.size()]);
    }
    
    public ActionForward getInputForward() 
    {
        if (this.getModuleConfig().getControllerConfig().getInputForward())
            return findForward(this.getInput());
        return new ActionForward(this.getInput());
    }
}

ActionForward类
从以上的Action类的讨论中可知,execute()方法返回一个ActionForward对象。ActionForward对象代表一个Web资源的逻辑抽象表示形式。这里的Web资源通常就是JSP页面或Java   Servlet。
ActionForward是该资源包的包装类,所以应用程序和实际资源之间并无多少瓜葛。实际的Web资源只在配置文件struts-config.xml中指定,并非在程度代码中写入。RequestDispatcher会根据redirect属性的值,来决定ActionForward实例要进行转发还是重定向。
要从一个Action实例返回一个ActionForward实例,可以在Action类内动态地创建一个ActionForward实例,或者更常见的做法是使用ActionMapping的findForward()方法找出配置文件中预先配置的一个ActionForward实例,如下所示:
return mapping.findForward("Success");
其中,mapping是一个ActionMapping实例。该程序片断能够返回一个参数"Success"对应的ActionForward实例。以下代码是在配置文件struts-config.xml中定义的forward元素:

 <action
      attribute="studentForm"
      input="/register.jsp"
      name="studentForm"
      path="/student"
      scope="request"
      validate="true"
      type="struts.action.StudentAction" >
      <forward name="Success" path="/registerOK.jsp" />
      </action>

ActionMapping类的findForward()方法首先会调用findForwardConfig()方法,以查看在<action>元素中是否包含<forward>子元素。如果有,就会检查<global-forwards>元素片断。一旦找到匹配的ActionForward实例,就会从execute()方法将其返回给RequestProcessor。下面是ActionMapping类的findForward()方法:

public ActionForward findForward(String name)
        {
            ForwardConfig config = this.findForwardConfig(name);
            if (config == null)
                config = this.getModuleConfig().findForwardConfig(name);
            return (ActionForward) config;
        }

stuts1:(Struts)Action类及其相关类的更多相关文章

  1. List 接口以及实现类和相关类源码分析

    List 接口以及实现类和相关类源码分析 List接口分析 接口描述 用户可以对列表进行随机的读取(get),插入(add),删除(remove),修改(set),也可批量增加(addAll),删除( ...

  2. SQL Server数据库读取数据的DateReader类及其相关类

    之前学了几天的SQL Server,现在用C#代码连接数据库了. 需要使用C#代码连接数据库,读取数据. 涉及的类有: ConfigurationManage SqlConnection SqlCom ...

  3. 《转》深入理解Activity启动流程(二)–Activity启动相关类的类图

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 在介绍Activity的详细启动流程之前,先为大家介绍Act ...

  4. 深入理解Activity启动流程(二)–Activity启动相关类的类图

    本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 在介绍Activity的详细启动流程之前,先 ...

  5. Web---演示Servlet的相关类、表单多参数接收、文件上传简单入门

    说明: Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代 ...

  6. Web---演示Servlet的相关类、下载技术、线程问题、自定义404页面

    Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代表用户的 ...

  7. aspnetcore 认证相关类简要说明一

    首先我想要简要说明是AuthenticationScheme类,每次看到Scheme这个单词我就感觉它是一个很高大上的单词,其实简单翻译过来就是认证方案的意思.既然一种方案,那我们就要知道这个方案的名 ...

  8. Java并发包——线程安全的Collection相关类

    Java并发包——线程安全的Collection相关类 摘要:本文主要学习了Java并发包下线程安全的Collection相关的类. 部分内容来自以下博客: https://www.cnblogs.c ...

  9. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

随机推荐

  1. UIAlertView与UIActionSheet

    1.UIAlertView(警告框) 1.1 创建警告框,设置样式 - (IBAction)alertView:(UIButton *)sender {//创建button按钮 //创建警告框的实例 ...

  2. ios 动态修改UILabel字体大小

    - (IBAction)sliderChange:(id)sender {   NSLog(@"sliderChange");   UISlider *slider = (UISl ...

  3. AFNetWorking 关于manager.requestSerializer.timeoutInterval 不起作用的问题

    之前一直遇到关于AFNetWorking请求时间设置了但是不起作用的情况,现用如下方式设置AF的超市时间即可. [manager.requestSerializer willChangeValueFo ...

  4. for循环执行顺序

    for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression4; } 执行的顺序应该是: 1)第一次循环,即初始化循环 ...

  5. 如何判断CPU的位数

    CPU是16位,32位,还是64位,主要指的是数据总线(data bus)有多少位,16位数据总线表示CPU一次可以从内存取2个byte的数据,32位数据总线表示CPU一次可以从内存取4byte数据, ...

  6. jquery获取当前鼠标所在位置的坐标

    $(document).ready(function(){ $(document).mousemove(function(e){ $('#xy').html("X :"+e.pag ...

  7. Ecstore中如何调用发起Ajax请求

    Ecstore的JS框架使用了mootools,所以ajax调用也使用mootools中的Request组件. 语法: var myRequest = new Request([options]); ...

  8. Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

    1,引言 在Python网络爬虫内容提取器一文我们详细讲解了核心部件:可插拔的内容提取器类gsExtractor.本文记录了确定gsExtractor的技术路线过程中所做的编程实验.这是第二部分,第一 ...

  9. STC10F10XE定时器中断输出10KHz的方波程序

    //咱做硬件的也动手做点测试程序,为了测试新做的电机驱动板,找了个51的板子当10K信号发生器测试IGBT开关延时时间. #include <STC_NEW_8051.H>#include ...

  10. 深入学习微框架Spring-boot

    深入学习微框架:Spring Boot 深入学习微框架:Spring Boot