`

struts form scope 原理

阅读更多
protected ActionForm processActionForm(HttpServletRequest request,    
                                           HttpServletResponse response,    
                                           ActionMapping mapping) {    
   
        // Create (if necessary) a form bean to use    
        //(如果有必要)创建一个新的formbean, 现在请看下面的RequestUtils.createActionForm源码     
        ActionForm instance = RequestUtils.createActionForm    
            (request, mapping, moduleConfig, servlet);    
        if (instance == null) {    
            return (null);    
        }    
   
        // Store the new instance in the appropriate scope    
    //把新的实例存储在合适的范围内(根据action配置的时指定的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);    
   
    }   


public static ActionForm createActionForm(    
            HttpServletRequest request,    
            ActionMapping mapping,    
            ModuleConfig moduleConfig,    
            ActionServlet servlet) {    
   
        // Is there a form bean associated with this mapping?    
        String attribute = mapping.getAttribute();    
        if (attribute == null) {    
            return (null);    
        }    
   
        // Look up the form bean configuration information to use    
        String name = mapping.getName();    
        FormBeanConfig config = moduleConfig.findFormBeanConfig(name);    
        if (config == null) {    
            log.warn("No FormBeanConfig found under '" + name + "'");    
            return (null);    
        }    
        //根据 action 配置从request或session中获得form bean 实例,如果指定scope="request"     
    //那么每次请求都是新的Request实例,那么也就不会有form bean 实例,该方法将返回null    
        ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());    
   
        // Can we recycle the existing form bean instance (if there is one)?    
    //可以重复使用一个已经存在的form bean 实例么    
        try {    
        //instance != null 且 instance 是 type 是 DynaActionForm 或 ActionForm 时返回form bean实例    
        //这里应该是scope="session"的情况吧,formbean保持上次请求的状态    
            if (instance != null && canReuseActionForm(instance, config)) {    
                return (instance);    
            }    
        } catch(ClassNotFoundException e) {    
            log.error(servlet.getInternal().getMessage("formBean", config.getType()), e);    
            return (null);    
        }    
   
        //返回一个新创建的form bean 实例()    
        return createActionForm(config, servlet);    
    }    
        
    private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope)    
    {    
        // Look up any existing form bean instance    
        if (log.isDebugEnabled()) {    
            log.debug(    
                    " Looking for ActionForm bean instance in scope '"   
                    + scope    
                    + "' under attribute key '"   
                    + attribute    
                    + "'");    
        }    
        ActionForm instance = null;    
        HttpSession session = null;    
        if ("request".equals(scope)) {    
            instance = (ActionForm) request.getAttribute(attribute);    
        } else {    
            session = request.getSession();    
            instance = (ActionForm) session.getAttribute(attribute);    
        }    
   
        return (instance);    
    }    

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics