深入浅出学习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中赋值了,具体怎么做的,请关注我的博客,慢慢道来!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深入浅出STRUTS 2 Struts Ti却发现了二者在技术与开发人员这两个层面上的共同之处,不久之后,两个项目就在WebWork的技术基础上进行了合并2。 当我们说起WebWork的时候,我们实际上说的是两个项目——XWork和WebWork。XWork是一个通用的命令框架,它提供了很多核心的功能,例如actions,验证和拦截器,它可以完全独立于执行上下文运行,并提供了一个内部的依赖注入机制,用来做配置和工厂实现的管理。 而WebWork则是一个完全独立的上下文。它用Web应用中运行所需的上下文把XWork包装起来,并提供了可以简化Web开发的特定实现。 Struts2的目标很简单——使Web开发变得更加容易。为了达成这一目标,Struts2中提供了很多新特性,比如智能的默认设置、annotation的使用以及“惯例重于配置”原则的应用,而这一切都大大减少了XML配置。Struts2中的Action都是POJO,这一方面增强了Action本身的可测试性,另一方面也减小了框架内部的耦合度,而HTML表单中的输入项都被转换成了恰当的类型以供action使用。开发人员还可以通过拦截器(可以自定义拦截器或者使用Struts2提供的拦截器)来对请求进行预处理和后处理,这样一来,处理请求就变得更加模块化,从而进一步减小耦合度。模块化是一个通用的主题——可以通过插件机制来对框架进行扩展;开发人员可以使用自定义的实现来替换掉框架的关键类,从而获得框架本身所不具备的功能;可以用标签来渲染多种主题(包括自定义的主题);Action执行完毕以后,可以有多种结果类型——包括渲染JSP页面,Velocity和Freemarker模板,但并不仅限于这些。最后,依赖注入也成了Struts2王国中的一等公民,这项功能是通过Spring框架的插件和Plexus共同提供的,与PicoContainer的结合工作还正在进行中。 本书的目的,是为了帮助读者掌握Struts2框架,并能够对组成框架的功能部件和可用的配置项有深刻的理解。我在书中还将介绍一些可以提高生产力的方法——包括默认配置项和应当注意的实现特性,可用的多种配置选项和一些开发技术。本书还会就与第三方软件进行集成的话题展开讨论。 2 Don Brown, Struts Ti项目的领导,他在文章中详细介绍了Struts Ti的历史

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值