struts源码初步研究

struts1.3.8源码初步学习
先介绍一下HttpServlet
一下的代码是有jd(反编译工具,个人觉得还是不错的)反编译过来的 仅供参考
HttpServlet
/*     */ public abstract class HttpServlet extends GenericServlet
/*     */   implements Serializable
/*     */ {
            //doGet方法
/*     */   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {

            }
            //doPost方法
/*     */   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 386 */     String protocol = req.getProtocol();
/* 387 */     String msg = lStrings.getString("http.method_post_not_supported");
/* 388 */     if (protocol.endsWith("1.1"))
/* 389 */       resp.sendError(405, msg);
/*     */     else
/* 391 */       resp.sendError(400, msg);
/*     */   }
           //doHead方法
/*     */   protected void doHead(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 311 */     NoBodyResponse response = new NoBodyResponse(resp);
/*     */
/* 313 */     doGet(req, response);
/* 314 */     response.setContentLength();
/*     */   }
            ...........
            //这里负责调用doGet或者doPost方法
/*     */   protected void service(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 733 */     String method = req.getMethod();
/*     */
/* 735 */     if (method.equals("GET")) {
/* 736 */       long lastModified = getLastModified(req);
/* 737 */       if (lastModified == -1L)
/*     */       {
/* 740 */         doGet(req, resp);
/*     */       } else {
/* 742 */         long ifModifiedSince = req.getDateHeader("If-Modified-Since");
/* 743 */         if (ifModifiedSince < lastModified / 1000L * 1000L)
/*     */         {
/* 747 */           maybeSetLastModified(resp, lastModified);
/* 748 */           doGet(req, resp);
/*     */         } else {
/* 750 */           resp.setStatus(304);
/*     */         }
/*     */       }
/*     */     }
/* 754 */     else if (method.equals("HEAD")) {
/* 755 */       long lastModified = getLastModified(req);
/* 756 */       maybeSetLastModified(resp, lastModified);
/* 757 */       doHead(req, resp);
/*     */     }
/* 759 */     else if (method.equals("POST")) {
/* 760 */       doPost(req, resp);
/*     */     }
/* 762 */     else if (method.equals("PUT")) {
/* 763 */       doPut(req, resp);
/*     */     }
/* 765 */     else if (method.equals("DELETE")) {
/* 766 */       doDelete(req, resp);
/*     */     }
/* 768 */     else if (method.equals("OPTIONS")) {
/* 769 */       doOptions(req, resp);
/*     */     }
/* 771 */     else if (method.equals("TRACE")) {
/* 772 */       doTrace(req, resp);
/*     */     }
/*     */     else
/*     */     {
/* 780 */       String errMsg = lStrings.getString("http.method_not_implemented");
/* 781 */       Object[] errArgs = new Object[1];
/* 782 */       errArgs[0] = method;
/* 783 */       errMsg = MessageFormat.format(errMsg, errArgs);
/*     */
/* 785 */       resp.sendError(501, errMsg);
/*     */     }
/*     */   }

/*     */   }

下面开始介绍一些struts的源码,来自struts-1.3.8-all.zip
1.action
public class Action {
  .....
      public ActionForward execute(ActionMapping mapping, ActionForm form,
        ServletRequest request, ServletResponse response)
        throws Exception {
        try {
            return execute(mapping, form, (HttpServletRequest) request,
                (HttpServletResponse) response);
        } catch (ClassCastException e) {
            return null;
        }
    }
    //每个action都要实现这个方法
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        return null;
    }
  ....
}
2.actionServlet
public class ActionServlet extends HttpServlet {
....
    public void init() throws ServletException {
    ....
    }
   
    public void destroy() {
    ....
    }
   
     /**
     * <p>Perform the standard request processing for this request, and create
     * the corresponding response.</p>
     *
     * @param request  The servlet request we are processing
     * @param response The servlet response we are creating
     * @throws IOException      if an input/output error occurs
     * @throws ServletException if a servlet exception is thrown
     */
    //多有的方法doGet,doPost方法都走这里,并交给processor去调用处理相关
    protected void process(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
        ModuleUtils.getInstance().selectModule(request, getServletContext());

        ModuleConfig config = getModuleConfig(request);

        RequestProcessor processor = getProcessorForModule(config);

        if (processor == null) {
            processor = getRequestProcessor(config);
        }
        //在这里,交由RequestProcessor去处理,下面会详细说明
        processor.process(request, response);
    }
   
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        //注意这里
        process(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        //同样注意这里
        process(request, response);
    }
....
}

3.RequestProcessor
public class RequestProcessor {
....
    public void process(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        // Wrap multipart requests with a special wrapper
        request = processMultipart(request);

        // Identify the path component we will use to select a mapping
        String path = processPath(request, response);

        if (path == null) {
            return;
        }

        if (log.isDebugEnabled()) {
            log.debug("Processing a '" + request.getMethod() + "' for path '"
                + path + "'");
        }

        // Select a Locale for the current user if requested
        processLocale(request, response);

        // Set the content type and no-caching headers if requested
        processContent(request, response);
        processNoCache(request, response);

        // General purpose preprocessing hook
        if (!processPreprocess(request, response)) {
            return;
        }

        this.processCachedMessages(request, response);

        // Identify the mapping for this request
        ActionMapping mapping = processMapping(request, response, path);

        if (mapping == null) {
            return;
        }

        // Check for any role required to perform this action
        if (!processRoles(request, response, mapping)) {
            return;
        }

        // Process any ActionForm bean related to this request
        ActionForm form = processActionForm(request, response, mapping);

        processPopulate(request, response, form, mapping);

        // Validate any fields of the ActionForm bean, if applicable
        try {
            if (!processValidate(request, response, form, mapping)) {
                return;
            }
        } catch (InvalidCancelException e) {
            ActionForward forward = processException(request, response, e, form, mapping);
            processForwardConfig(request, response, forward);
            return;
        } catch (IOException e) {
            throw e;
        } catch (ServletException e) {
            throw e;
        }

        // Process a forward or include specified by this mapping
        if (!processForward(request, response, mapping)) {
            return;
        }

        if (!processInclude(request, response, mapping)) {
            return;
        }

        // Create or acquire the Action instance to process this request
        Action action = processActionCreate(request, response, mapping);

        if (action == null) {
            return;
        }

        //这里是重点
        // Call the Action instance itself
        ActionForward forward =
            processActionPerform(request, response, action, form, mapping);

        // Process the returned ActionForward instance
        processForwardConfig(request, response, forward);
    }
    //这个方法调用action的execute的方法
    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));
        }
    }
   
...
}

 

你可能感兴趣的:(exception,struts,servlet,processing,action,反编译工具)