`

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=...

    Struts framework的工作原理和组件Struts framework的工作原理和组件

    ### Struts Framework 的工作原理与核心组件解析 #### 一、概述 Struts框架作为Java Web开发中的一个重要组成部分,自2000年初发布以来,便因其强大的功能和易用性而受到广泛欢迎。该框架主要针对MVC(Model-View-...

    ActionForm_Scope为Session

    标题“ActionForm_Scope为Session”涉及到的是Struts框架中的一个关键概念,即ActionForm对象的作用域设置。在Struts框架中,ActionForm是用于封装请求参数并传递给业务逻辑层(通常为Servelets或JavaBeans)的模型...

    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...

    集成Tomcat与Struts

    - 例如,配置一个Action映射:`<action path="/example" type="com.example.ExampleAction" name="exampleForm" input="/example.jsp" scope="request" parameter="exampleParam">` 3. **编写Struts应用程序**: ...

    struts1.2.9包下载

    <action path="/login" type="com.example.struts.LoginAction" name="loginForm" scope="request" input="/login.jsp"> </struts-config> ``` ### 核心概念详解 #### 1. 模型(Model) 模型层主要负责...

    Struts技术

    - **工作原理**:Struts1.x使用ActionServlet作为中心控制器,而Struts2.x采用拦截器机制。 - **配置方式**:Struts1.x依赖XML文件进行配置,而Struts2.x除了XML配置外,还支持注解配置。 - **MVC实现**:Struts1.x...

    Struts1的开发过程

    3. 配置`struts-config.xml`:这是Struts的核心配置文件,用于定义Action、Form Bean、Action Mapping等。一个简单的配置示例如下: ```xml <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD ...

    Struts1基本配置

    - **struts-config.xml**:这是Struts1的核心配置文件,包含了所有Action、Form Bean、Forward、Plug-in等配置信息。例如: ```xml <struts-config> <form-beans> <!-- Form Bean配置 --> </form-beans> <!-...

    struts2学习心得

    例如:public class HelloWordAction extends Action{public ActionForward execute(ActionMapping mapping, ActionForm form, HttpRequest request, HttpResponse response)throws Exception{。。。。}} 而Struts2...

    struts多附件上传

    <action path="/upload" type="com.example.UploadAction" name="uploadForm" scope="request"> ``` 五、安全性与优化 1. 安全检查:确保对上传的文件进行大小、类型检查,防止文件注入攻击。 2. 临时文件管理:...

    Struts学习总结.doc

    例如,`<action>`标签内的`path`指定了URL路径,`type`指定了Action的类,`name`对应Form Bean,`scope`定义了Form Bean的作用域,`input`用于指定错误时回跳的页面,而`validate`标记是否执行表单验证。 2. **...

    Struts1.x.pdf

    的路径,在form的action中写的那个路径" type="Action的类全名" name="form的名字" scope="request|session(默认值)"> 在自定义的Action中使用的findForward方法的参数" path="跳转页面的路径" redirect="false"/>...

    struts1.2文件上传demo

    <action path="/upload" type="com.yourpackage.UploadAction" name="uploadForm" scope="request"> ``` 2. **创建UploadForm**: 创建一个名为`UploadForm`的类,它继承自`ActionForm`,并包含`File`和`...

    struts1实现的文件批量上传

    <action path="/upload" type="ResourceAction" name="batchUploadForm" parameter="method" scope="request"> ``` 最后,我们来到Action类,处理上传请求。在Action中,我们需要获取ActionForm中的文件列表...

    Struts1实现的文件上传

    Struts1是一个经典的Java Web框架,它为开发者提供了一种结构化的MVC(Model-View-Controller)设计模式实现方式,极大地简化了Web...然而,了解Struts1的文件上传对于理解MVC模式和Web开发的基本原理仍然很有价值。

    struts1小例子

    <action path="/hello" type="com.example.HelloWorldAction" name="helloForm" scope="request"> </struts-config> ``` 3. **创建ActionForm**:如`HelloWorldForm.java` ```java public class ...

    Struts配置文件详解.

    `<form-bean>`还可以包含其他属性,如`type`来指定自定义的ActionForm类,以及`scope`来确定ActionForm的生存范围(例如request或session)。 5. 其他元素 - `<action>`元素定义Action的映射,包括Action的类、...

Global site tag (gtag.js) - Google Analytics