ActionServlet的执行流程(源码)

这里写图片描述

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
       type="com.struts.LoginAction"
       name="loginForm"
       scope="request"
       validate="false"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>

初始化:在容器启动时将自动完成包含以下的工作

ModuleConfig
实现代码:

public ModuleConfigImpl(String prefix) {
        super();
        this.prefix = prefix;
        this.actionConfigs = new HashMap();
        this.actionConfigList = new ArrayList();
        this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
        this.actionMappingClass = "org.apache.struts.action.ActionMapping";
        this.actionForwardClass = "org.apache.struts.action.ActionForward";
        this.configured = false;
        this.controllerConfig = null;
        this.dataSources = new HashMap();
        this.exceptions = new HashMap();
        this.formBeans = new HashMap();
        this.forwards = new HashMap();
        this.messageResources = new HashMap();
        this.plugIns = new ArrayList();
    }
String path = processPath(request, response);
 实现代码:

path = request.getServletPath();
path =="/login.do"
  int slash = path.lastIndexOf("/");
    int period = path.lastIndexOf(".");
        if ((period >= 0) && (period > slash)) {
            path = path.substring(0, period);
        }
        return (path);
path="/login"
ActionMapping mapping = processMapping(request, response, path);
实现代码:

ActionMapping mapping = (ActionMapping)
     moduleConfig.findActionConfig(path);
从ActionConfigs的map中通过path取得对应的ActionMapping对象
<action-mappings>
<action path="/login"
       type="com.struts.LoginAction"
       name="loginForm"
       scope="request"
       validate="true"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
ActionForm form = processActionForm(request, response, mapping);
实现代码:

ActionForm instance = RequestUtils.createActionForm()//创建ActionForm
if ("request".equals(mapping.getScope())) {
            request.setAttribute(mapping.getAttribute(), instance);
        } else {
            HttpSession session = request.getSession();
            session.setAttribute(mapping.getAttribute(), instance);
        }
        return (instance);
        ------------------------------
       1)public static ActionForm createActionForm()  
       实现代码:
       String attribute = mapping.getAttribute();
        if (attribute == null) {
            return (null);
        }
        String name = mapping.getName();

     //从FormBeans的MAP中通过name取得对应的FormBeanConfig对象
        FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
        if (config == null) {
            return (null);

      // 从Scope中取得ActionForm
      2)ActionForm instance = lookupActionForm(request, attribute,           
        mapping.getScope());
     实现代码:
       {if ("request".equals(scope)) {
            instance = (ActionForm) request.getAttribute(attribute);
        } else {
            session = request.getSession();
            instance = (ActionForm) session.getAttribute(attribute);
        }
        return (instance);}
        if (instance != null && canReuseActionForm(instance, config)) {
                return (instance);
            }

        return createActionForm(config, servlet);//反射方式生成ActionForm对象
processPopulate(request, response, form, mapping);
 实现代码:

  if (form == null) {
            return;
        }
form.reset(mapping, request);//ActionForm初始化
//ActionForm对象自动收集请求参数
  RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix()
                              request);
    ---------------------
    public static void populate()//自动收集请求参数
    实现代码:
    HashMap properties = new HashMap();
    Enumeration names = null;
    names = request.getParameterNames();
    while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String stripped = name;
   Object parameterValue = null;
   parameterValue = request.getParameterValues(name);
   properties.put(stripped, parameterValue);
   BeanUtils.populate(bean, properties);
   ----------------
   public static void populate(Object bean, Map properties)
   实现代码:
    if ((bean == null) || (properties == null)) {
            return;
    Iterator names = properties.keySet().iterator();//取得参数名
    while (names.hasNext()) {
            String name = (String) names.next();
            Object value = properties.get(name);
            setProperty(bean, name, value);
        }
//对ActionForm信息进行验证
if (!processValidate(request, response, form, mapping)) {
            return;
        }
//创建action对象
 protected HashMap actions = new HashMap();
Action action = processActionCreate(request, response, mapping);
代码实现:

protected Action processActionCreate(HttpServletRequest request,
                                         HttpServletResponse response,
                                         ActionMapping mapping)
  {String className = mapping.getType();
Action instance = null;
        synchronized (actions) {
            instance = (Action) actions.get(className);
            if (instance != null) {
               return (instance);
            }
     instance = (Action) RequestUtils.applicationInstance(className);
     actions.put(className, instance);
     return (instance);
   }
//执行Action
6、ActionForward forward= 
processActionPerform(request, response,action, form, mapping);

代码实现:

protected ActionForward
        processActionPerform(HttpServletRequest request,
                             HttpServletResponse response,
                             Action action,
                             ActionForm form,
                             ActionMapping mapping)
        throws IOException, ServletException {
        try {
            return (action.execute(mapping, form, request, response));
        } catch (Exception e) {
            return (processException(request, response,
                                     e, form, mapping));
        }
    }
processForwardConfig(request, response, forward);

代码实现:

protected void processForwardConfig(HttpServletRequest request,
                                        HttpServletResponse response,
                                        ForwardConfig forward)
        throws IOException, ServletException {
        if (forward == null) {
            return;
        }
    String forwardPath = forward.getPath();
     uri = forwardPath;
   if (forward.getRedirect()) {

            response.sendRedirect(response.encodeRedirectURL(uri));
   else {
            doForward(uri, request, response);
        }


代码实现:
protected void doForward(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
{RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
rd.forward(request, response);

猜你喜欢

转载自blog.csdn.net/Pluto__lxh/article/details/82107833
今日推荐