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

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

 

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


[java]  view plain copy print ?
  1. /** 
  2.  
  3.      * <p>Retrieve and return the <code>ActionForm</code> associatedwith 
  4.  
  5.      * this mapping, creating and retaining oneif necessary. If there is no 
  6.  
  7.      * <code>ActionForm</code> associated with this mapping,return 
  8.  
  9.      * <code>null</code>.</p> 
  10.  
  11.      * 
  12.  
  13.      * @param request The servlet request weare processing 
  14.  
  15.      * @param response The servlet response weare creating 
  16.  
  17.      * @param mapping The mapping we are using 
  18.  
  19.      */  
  20.   
  21.    protectedActionForm processActionForm(HttpServletRequestrequest,  
  22.   
  23.                                           HttpServletResponse response,  
  24.   
  25.                                           ActionMapping mapping) {  
  26.   
  27.    
  28.   
  29.         // Create (if necessary) a form bean to use  
  30.   
  31.         ActionForm instance = RequestUtils.createActionForm  
  32.   
  33.             (request, mapping, moduleConfig, servlet);  
  34.   
  35.         if (instance == null) {  
  36.   
  37.             return (null);  
  38.   
  39.         }  
  40.   
  41.    
  42.   
  43.         // Store the new instance in the appropriate scope  
  44.   
  45.         if (log.isDebugEnabled()) {  
  46.   
  47.             log.debug(" Storing ActionForm bean instance in scope '" +  
  48.   
  49.                 mapping.getScope() + "' under attribute key '" +  
  50.   
  51.                 mapping.getAttribute() + "'");  
  52.   
  53.         }  
  54.   
  55.         if ("request".equals(mapping.getScope())) {  
  56.   
  57.            request.setAttribute(mapping.getAttribute(), instance);  
  58.   
  59.         } else {  
  60.   
  61.             HttpSession session =request.getSession();  
  62.   
  63.            session.setAttribute(mapping.getAttribute(), instance);  
  64.   
  65.         }  
  66.   
  67.         return (instance);  
  68.   
  69.    
  70.   
  71. }  


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

 

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

 

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

 

        第一个步骤就是创建ActionForm


[java]  view plain copy print ?
  1. // Create (if necessary) a formbean to use  
  2.   
  3.         ActionForm instance = RequestUtils.createActionForm  
  4.   
  5.             (request, mapping, moduleConfig, servlet);  
  6.   
  7.         if (instance == null) {  
  8.   
  9.             return (null);  
  10.   
  11.        }  
  12.   
  13.   
  14.   
  15.    


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


[java]  view plain copy print ?
  1. publicstaticActionForm createActionForm(  
  2.   
  3.             HttpServletRequest request,  
  4.   
  5.             ActionMapping mapping,  
  6.   
  7.             ModuleConfig moduleConfig,  
  8.   
  9.             ActionServlet servlet) {  
  10.   
  11.    
  12.   
  13.         // Is there a form bean associated with this mapping?  
  14.   
  15.         String attribute = mapping.getAttribute();  
  16.   
  17.         if (attribute == null) {  
  18.   
  19.             return (null);  
  20.   
  21.         }  
  22.   
  23.    
  24.   
  25.         // Look up the form bean configuration information to use  
  26.   
  27.         String name = mapping.getName();  
  28.   
  29.         FormBeanConfig config =moduleConfig.findFormBeanConfig(name);  
  30.   
  31.         if (config == null) {  
  32.   
  33.             log.warn("No FormBeanConfig found under '"+ name + "'");  
  34.   
  35.             return (null);  
  36.   
  37.         }  
  38.   
  39.    
  40.   
  41.         ActionForm instance = lookupActionForm(request,attribute, mapping.getScope());  
  42.   
  43.    
  44.   
  45.         // Can we recycle the existing form bean instance (if there is one)?  
  46.   
  47.         try {  
  48.   
  49.             if (instance != null && canReuseActionForm(instance,config)) {  
  50.   
  51.                 return (instance);  
  52.   
  53.             }  
  54.   
  55.         } catch(ClassNotFoundException e) {  
  56.   
  57.             log.error(servlet.getInternal().getMessage("formBean",config.getType()), e);  
  58.   
  59.             return (null);  
  60.   
  61.         }  
  62.   
  63.    
  64.   
  65.         return createActionForm(config,servlet);  
  66.   
  67. }  

     

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

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

[html]  view plain copy print ?
  1.    
  2.   
  3. <form-beans>  
  4.   
  5.        <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>  
  6.   
  7.     </form-beans>  


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

 

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


[java]  view plain copy print ?
  1. if ("request".equals(scope)){  
  2.   
  3.             instance = (ActionForm)request.getAttribute(attribute);  
  4.   
  5.         } else {  
  6.   
  7.             session = request.getSession();  
  8.   
  9.             instance = (ActionForm)session.getAttribute(attribute);  
  10.   
  11.         }  


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

 

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


[java]  view plain copy print ?
  1. if ("request".equals(mapping.getScope())){  
  2.   
  3.            request.setAttribute(mapping.getAttribute(), instance);  
  4.   
  5.         } else {  
  6.   
  7.             HttpSession session =request.getSession();  
  8.   
  9.            session.setAttribute(mapping.getAttribute(), instance);  
  10.   
  11.         }  

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值