`
dowhathowtodo
  • 浏览: 777362 次
文章分类
社区版块
存档分类
最新评论

深入浅出学习Struts框架(十一)-利用ActionMapping来创建ActionForm

 
阅读更多

最近一段时间更新博客不太频繁,struts系列文章一直没有更新。主要是忙于权限项目,关于权限项目的博客也在构思中,过不了几日就能让各位读者看到。

今天主要是讲解ActionServlet中的一个方法processActionForm,当我们在截取字符串,再根据字符串取得ActionMapping(这是前两篇文章中介绍的)之后,我们就要用利用ActionMapping来创建ActionForm了,并且把ActionForm放到request或session中管理。

先来看具体struts中processActionForm方法的具体实现:


/**

     * <p>Retrieve and return the <code>ActionForm</code> associatedwith

     * this mapping, creating and retaining oneif necessary. If there is no

     * <code>ActionForm</code> associated with this mapping,return

     * <code>null</code>.</p>

     *

     * @param request The servlet request weare processing

     * @param response The servlet response weare creating

     * @param mapping The mapping we are using

     */

   protectedActionForm processActionForm(HttpServletRequestrequest,

                                          HttpServletResponse response,

                                          ActionMapping mapping) {

 

        // Create (if necessary) a form bean to use

        ActionForm instance = RequestUtils.createActionForm

            (request, mapping, moduleConfig, 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);

 

}


这个方法的大体流程是:根据ActionMapping中的name名称查找ActionForm,如果配置了ActionForm,那么就到requestsession中查找,如果在requestsession中存在已经创建的ActionForm,那么将返回。如果不存在那么会根据ActionForm的完成路径采用反射进行创建,再将创建好的ActionForm放到requestsession中,之后返回ActionForm

具体我们可以跟随断点调试来看看这个方法是如何运行的。

首先还是同上篇博客中的方法一样,先设置断点,之后进入processActionForm方法。

第一个步骤就是创建ActionForm


// Create (if necessary) a formbean to use

        ActionForm instance = RequestUtils.createActionForm

            (request, mapping, moduleConfig, servlet);

        if (instance == null) {

            return (null);

       }



 


通过调用RequestUtils.createActionForm的方法把ActionMapping中的ActionForm字符串生成对象,并且返回。进入这段代码中:


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

        }

 

        ActionForm instance = lookupActionForm(request,attribute, mapping.getScope());

 

        // Can we recycle the existing form bean instance (if there is one)?

        try {

            if (instance != null && canReuseActionForm(instance,config)) {

                return (instance);

            }

        } catch(ClassNotFoundException e) {

            log.error(servlet.getInternal().getMessage("formBean",config.getType()), e);

            return (null);

        }

 

        return createActionForm(config,servlet);

}




方法首先定义变量nama,并且从mapping中获取值,String name = mapping.getName();也就是我们实例中的LoginForm字符串。之后通过调用FormBeanConfig config =moduleConfig.findFormBeanConfig(name);这句话把相应的LoginForm字符串生成相应的对象。

这里要说明的是我们在struts-config配置文件中,配置过这样一个标签信息:

 

<form-beans>

       <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>

    </form-beans>


这个标签在服务器一启动的时候就会利用digester读取这里的配置信息,并且放在FormBeanConfig类中,这样我们可以通过上面那一句话就可以把LoginForm字符串生成相应的对象。

之后调用了ActionForm instance = lookupActionForm(request,attribute, mapping.getScope());这个方法,这个方法主要是查找scope属性中有没有存在ActionForm。具体实现:


if ("request".equals(scope)){

            instance = (ActionForm)request.getAttribute(attribute);

        } else {

            session = request.getSession();

            instance = (ActionForm)session.getAttribute(attribute);

        }


这里判断scope属性值是否为request,如果是则从request中读出ActionForm,如果不是则从session中读出。程序如果是第一次执行,那么ActionForm会是为空的。因为这里的ActionForm为空,所以就进入了if判断语句中,最后通过调用return createActionForm(config, servlet);创建ActionForm并且返回。

之后processActionForm就会把返回来的ActionForm放入request或者session中。具体实现就是:


if ("request".equals(mapping.getScope())){

           request.setAttribute(mapping.getAttribute(), instance);

        } else {

            HttpSession session =request.getSession();

           session.setAttribute(mapping.getAttribute(), instance);

        }

到此为止,ActionForm就创建完成,当ActionForm创建完成之后,就要用其他的方法来往ActionForm中赋值了,具体怎么做的,请关注我的博客,慢慢道来!


分享到:
评论

相关推荐

    Struts框架及标记库

    1 Struts框架 3 1.1 Struts压缩包内容 3 1.2 Struts体系结构 4 1.3 Struts框架中的组件 5 1.3.1 Struts配置文件 5 1.3.2 ActionServlet类 8 1.3.3 ActionMapping类 10 1.3.4 Action类 10 1.3.5 ActionForm类 11 ...

    Struts简介 什么是Struts Struts基本运作流程

    Struts简介 什么是Struts Struts基本运作流程 ActionMapping类 Action类 ActionForm类 ActionError与ActionMessage 协同开发 模块化程序 Struts异常处理 Struts国际化支持 PlugIn接口 等等

    第一个structs

    自己写的第一个structs 示例 体会到sturts的工作流程--IE.request-- ActionServlet.struts-config.xml Action --ActionMapping(ActionForm Bean); 继续向大家学习!

    org.apache.struts缺少所需包

    import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction等缺少

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

    4 import org.apache.struts.action.ActionMapping; 5 6 /** 7 * MyEclipse Struts 8 * Creation date: 11-10-2005 9 * 10 * XDoclet definition: 11 * @struts.form name="LoginForm" 12 */ 13 public ...

    利用SSH框架做的登录小例子

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LoginForm loginForm = (LoginForm) form;// TODO Auto-generated ...

    structs程序设计从入门到精通word文档

    第2章深入Struts结构 8 2.1人介绍应用框架 mvc和model2 8 2.2 Struts原理、开发及项目实施 8 2.3使用STRUST控制流 8 2.4讨论STRUTS的优缺点 9 2.5 Struts入门经验 9 2.6用strtus制作登陆页面: 9 第3章Struts 基础...

    Struts in Action[文字版][中文]

    书名:Struts in Action[文字版][中文](电子书) 格式:PDF 本书的目的是帮助Web应用开发者能够最好的使用 Struts web 应用框架。 Struts是一个开源软件,有助于开发者更加快速和容易地建立Web应用程序。Struts依靠...

    POI导入导出EXCEL文件.(struts 1.x and struts2.x).doc

    struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....

    struts中文学习资料.doc

    Struts框架 1. struts压缩包内容………………………………………………………………………3 2. struts体系结构(模型-视窗-控制器)…………………………………………………3 3. struts框架的组件…………………………...

    Struts1教程之ActionMapping_动力节点Java学院整理

    主要介绍了Struts1教程之ActionMapping,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Struts中文手册[文字版][中文]

    书名:Struts中文手册[文字版][中文](电子书) 格式:PDF 1. Struts 框架1 1.1. Struts 压缩包内容.1 1.2. Struts 体系结构1 1.2.1. 模型.2 1.2.2. 视窗.2 1.2.3. 控制器.2 1.3. Struts 框架中的组件.3 1.3.1. ...

    Struts原理、开发及项目实施

    Struts原理、开发及项目实施 Holen 2002-9-12 &lt;br/&gt;1、 摘要 2、 关键词 3、 Framework 4、 Struts的起源 5、 Struts工作原理 6、 Struts安装 7、 一个实例 8、 Struts优缺点...

    外文翻译 stus MVC

    The struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings. (If you have not noticed it, classes that end with s are ...

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

     以上是Spring+Hibernate将文件二进制数据持久化到数据库的解决方案,而Struts通过将表单中file类型的组件映射为ActionForm中类型为org.apache.struts.upload. FormFile的属性来获取表单提交的文件数据。  工程...

    struts1流程和原理

    struts1的几个核心组件是值得我们注意的: 1 、ActionServlet(核心控制器)。 2、RequestProcessor类(处理异常的核心组件)。 3、ActionForm(接收页面中传过的数据)。 4、Action(是控制器,主要是从ActionForm中...

    从Java走向Java+EE+.rar

    9.1.1 Struts框架的出现及其优点 107 9.1.2 Struts的MVC框架 108 9.2 Struts的配置 110 9.2.1 配置Web.xml 110 9.2.2 配置Struts-config.xml 111 9.2.3 多个配置文件的使用 120 9.3 Struts的各种组件 ...

    【项目实战案例】java校园订餐系统项目(web端)

    【项目实战案例】java校园订餐系统项目...import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.bean.HzpBean;

    Spring面试题

    用户提交表单时,一个配置好的ActionForm对象被创建,并被填入表单相应的数据,ActionServler根据Struts-config.xml文件配置好的设置决定是否需要表单验证,如果需要就调用ActionForm的 Validate()验证后选择将...

    Struts framework

    在Struts framework中, Controller主要是ActionServlet,但是对于业务逻辑的操作则主要由Action、ActionMapping、ActionForward这几个组件协调完成(也许这几个组件,应该划分到模型中的业务逻辑一块)。...

Global site tag (gtag.js) - Google Analytics