strutsr源码解读

类别:Java 点击:0 评论:0 推荐:

html:form

org.apache.struts.action.ActionServlet;

ActionServlet extends HttpServlet

doGet
doPost

 process(request, response);
 {
 processor = getRequestProcessor(config);
 processor.process(request, response);
 }
 
 RequestProcessor 没有继承任何的类,只是一个repquest的处理器,逻辑封装
 方法
 public void process(HttpServletRequest request,
                        HttpServletResponse response)
                        {
      //封装request                 
      request = processMultipart(request);
      /*
      详细:
      protected HttpServletRequest processMultipart(HttpServletRequest request) {

        if (!"POST".equalsIgnoreCase(request.getMethod())) {
            return (request);
        }
       
        String contentType = request.getContentType();
        if ((contentType != null) &&
            contentType.startsWith("multipart/form-data")) {
            return (new MultipartRequestWrapper(request));
        } else {
            return (request);
        }

    }
      */  
     
      // Identify the path component we will use to select a mapping
        String path = processPath(request, response);
        if (path == null) {
            return;
        }
       
        //begin 可以忽略       
          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);
       //end 忽略
      
      
          // Identify the mapping for this request
        ActionMapping mapping = processMapping(request, response, path);
        if (mapping == null) {
            return;
        }
        /*详细
        根据path找到mapping
        */

        // Check for any role required to perform this action
        if (!processRoles(request, response, mapping)) {
            return;
        }
 
        //action form 的处理
        // Process any ActionForm bean related to this request
        ActionForm form = processActionForm(request, response, mapping);
        /*
        用mapping找actionform
         protected ActionForm processActionForm(HttpServletRequest request,
                                           HttpServletResponse response,
                                           ActionMapping mapping) {

        // Create (if necessary) a form bean to use
        ActionForm instance = RequestUtils.createActionForm
            (request, mapping, moduleConfig, servlet);
        /*详细
        ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
        if (instance != null && canReuseActionForm(instance, config)) {
                return (instance);
         否则
          return createActionForm(config, servlet);
        */
        if (instance == null) {
            return (null);
        }

        // Store the new instance in the appropriate scope
        if (log.isDebugEnabled()) {
            log.debug(" Storing ActionForm bean instance in scope '" +
                mapping.getScope() + "' under attribute key '" +
                mapping.getAttribute() + "'");
        }
        if ("request".equals(mapping.getScope())) {
            request.setAttribute(mapping.getAttribute(), instance);
        } else {
            HttpSession session = request.getSession();
            session.setAttribute(mapping.getAttribute(), instance);
        }
        return (instance);

    }

        */
        processPopulate(request, response, form, mapping);
        /*
        RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
                              request);

        */
        if (!processValidate(request, response, form, mapping)) {
            return;
        }
        /*
        protected boolean processValidate(HttpServletRequest request,
                                      HttpServletResponse response,
                                      ActionForm form,
                                      ActionMapping mapping)
        throws IOException, ServletException {

        if (form == null) {
            return (true);
        }
                                              // Was this request cancelled?
        if (request.getAttribute(Globals.CANCEL_KEY) != null) {
            if (log.isDebugEnabled()) {
                log.debug(" Cancelled transaction, skipping validation");
            }
            return (true);
        }

        // Has validation been turned off for this mapping?
        if (!mapping.getValidate()) {
            return (true);
        }

        // Call the form bean's validation method
        if (log.isDebugEnabled()) {
            log.debug(" Validating input form properties");
        }
        ActionMessages errors = form.validate(mapping, request);
        if ((errors == null) || errors.isEmpty()) {
            if (log.isTraceEnabled()) {
                log.trace("  No errors detected, accepting input");
            }
            return (true);
        }

        // Special handling for multipart request
        if (form.getMultipartRequestHandler() != null) {
            if (log.isTraceEnabled()) {
                log.trace("  Rolling back multipart request");
            }
            form.getMultipartRequestHandler().rollback();
        }

        // Was an input path (or forward) specified for this mapping?
        String input = mapping.getInput();
        if (input == null) {
            if (log.isTraceEnabled()) {
                log.trace("  Validation failed but no input form available");
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                               getInternal().getMessage("noInput",
                                                        mapping.getPath()));
            return (false);
        }

        // Save our error messages and return to the input form if possible
        if (log.isDebugEnabled()) {
            log.debug(" Validation failed, returning to '" + input + "'");
        }
        request.setAttribute(Globals.ERROR_KEY, errors);

        if (moduleConfig.getControllerConfig().getInputForward()) {
            ForwardConfig forward = mapping.findForward(input);
            processForwardConfig( request, response, forward);
        } else {
            internalModuleRelativeForward(input, request, response);
        }

        return (false);

    }

        */

        // 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);
                                 /*
                                   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));
        }

    }
                                 */

        // Process the returned ActionForward instance
        processForwardConfig(request, response, forward);
         protected void processForwardConfig(HttpServletRequest request,
                                        HttpServletResponse response,
                                        ForwardConfig forward)
        throws IOException, ServletException {

        if (forward == null) {
            return;
        }
       
        if (log.isDebugEnabled()) {
            log.debug("processForwardConfig(" + forward + ")");
        }
       
        String forwardPath = forward.getPath();
        String uri = null;
       
        // paths not starting with / should be passed through without any processing
        // (ie. they're absolute)
        if (forwardPath.startsWith("/")) {
            uri = RequestUtils.forwardURL(request, forward, null);    // get module relative uri
        } else {
            uri = forwardPath;
        }
       
        if (forward.getRedirect()) {
            // only prepend context path for relative uri
            if (uri.startsWith("/")) {
                uri = request.getContextPath() + uri;
            }
            response.sendRedirect(response.encodeRedirectURL(uri));
           
        } else {
            doForward(uri, request, response);
        }

    }

}

 protected void doForward(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
           
        // Unwrap the multipart request, if there is one.
        if (request instanceof MultipartRequestWrapper) {
            request = ((MultipartRequestWrapper) request).getRequest();
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
        if (rd == null) {
            response.sendError(
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                getInternal().getMessage("requestDispatcher", uri));
            return;
        }
        rd.forward(request, response);
    }

本文地址:http://com.8s8s.com/it/it11054.htm