`

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

分享到:
评论

相关推荐

    struts基于mvc的开发代码

    <form-bean name="testForm" type="com.yourcompany.struts.form.TestForm" /> <form-bean name="test1Form" type="com.yourcompany.struts.form.Test1Form" /> <form-bean name="test2Form" type=...

    struts1 demo

    <form-bean name="LoginForm" type="com.yza.struts.form.LoginForm"></form-bean> </form-beans> <action path="/login" name="LoginForm" type="com.yza.struts.action.LoginAction" scope="request...

    Struts原理、开发及项目实施

    RegUserForm "/> </form-beans> <br/><action-mappings><br/><action path="/regUserAction"<br/> type=" org.cjea.Struts.example.RegUserAction " attribute=" regUserForm " scope="request...

    struts配置元素详解

    ”1.0” encoding=”ISO-8859-1”?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" ...scope="" validate="" input="" / > </struts-config>

    struts2的入门开发

    struts2的入门开发,环境配置− <struts-config> ...<action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="test.action.LoginAction"> </struts-config>

    用Struts2新建一个应用的方法步骤

    <form action="login.action" method="post"> username: password: 5. 再新建一个JSP页面。File——new——JSP,在File Name中输入一个合法的名字。例如:result,在此页面<body>...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    11 * @struts.form name="LoginForm" 12 */ 13 public class LoginForm extends ActionForm { 14 15 /** password property */ 16 private String password; 17 18 /** username property */ 19 private String ...

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    <form-bean name="addForm" type="com.lmf118.struts.form.AddForm" /> </form-beans> attribute="addForm" input="/add.jsp" --错误时跳转的页面 name="addForm" --Frombean的名字 path="/add" ...

    Struts in Action中文版

    2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...

    外文翻译 stus MVC

    • Before passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on UserActionForm. Note: This is not always wise to do. There might be ways ...

    struts in Action

    1.1.1. 谁创建了Struts?...................................................................................................19 1.1.2. 为什么Struts 要开源?....................................................

    ssh(structs,spring,hibernate)框架中的上传下载

    Struts+Spring+Hibernate实现上传下载    本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文...

Global site tag (gtag.js) - Google Analytics