深入浅出学习Struts1框架(十)-ActionMapping的生成和查找

 前几篇博客主要介绍的ActionServlet的初始化和Struts1是如何完成截取字符串工作的,今天继续分析Struts1截取完字符串所要做的工作。


    在struts专栏的开篇mvc小实例中我们编写了ActionMapping这样一个类、struts-config.xml配置文件,在那时我们对截取的字符串匹配,利用dom4j来读取了配置文件的信息,并且把他以ActionMapping的形式保存在内存中。


    今天我们深入的来看看struts1是怎样拿到ActionMapping的,依旧和上篇的博客思路一样,利用断点调试的方式来进入源代码中,具体做法见上篇博客。


    紧接着上篇博客,我们的断点走出了processpath方法,

    

 

    在上一篇博客中我们已经讲解了,这个方法是用来截取字符串的,今天我们来看怎样获得ActionMapping的方法---processMapping。


    在此之前简单说一下ActionMapping,它的源代码中可以看出,其中最重要的属性和我们的mvc小实例中的ActionMapping类似,都是有path、type还有forwardMap,主要是对应的struts-config配置文件而来,这个就是保存这个配置文件的信息到内存中。


    具体的mvc小实例的ActionMapping代码如下:


[java]  view plain copy print ?
  1. package com.cjq.servlet;  
  2.   
  3. import java.util.Map;  
  4.   
  5. public class ActionMapping {  
  6.   
  7.     private String path;  
  8.       
  9.     private Object type;  
  10.       
  11.     private Map forwardMap;  
  12.   
  13.     public String getPath() {  
  14.         return path;  
  15.     }  
  16.   
  17.     public void setPath(String path) {  
  18.         this.path = path;  
  19.     }  
  20.   
  21.     public Object getType() {  
  22.         return type;  
  23.     }  
  24.   
  25.     public void setType(Object type) {  
  26.         this.type = type;  
  27.     }  
  28.   
  29.     public Map getForwardMap() {  
  30.         return forwardMap;  
  31.     }  
  32.   
  33.     public void setForwardMap(Map forwardMap) {  
  34.         this.forwardMap = forwardMap;  
  35.     }  
  36.       
  37. }  

    而Struts中的Actionconfig(因为ActionMapping是继承这个ActionConfig的,所以我们来看ActionConfig更加直接)的代码如下:

    


    

         

    从这两部分代码来看,更加印证了我在开篇写的mvc小实例是一个struts框架的雏形。


    讲完ActionMapping的一些内容后,相信对ActionMapping有所了解,那么系统是如何生成ActionMapping和如何找到ActionMapping的呢?这就是今天要说的整体:


    不知道读者还记不记得,我们在分析实例的第一篇的时候web.xml中有一个<load-on-startup>2</load-on-startup>  配置信息,这个信息就是说明了但服务器已启动就动态读取struts-config配置文件把配置文件的信息put到ActionMapping中。所以当我们运行服务器的时候,我们在内存中已经存在对应struts-config配置文件信息对应的ActionMapping。今天就是要通过processMapping读取这个ActionMapping类。

 

    进入断点调试,首先在processMapping方法上设置断点。

    


    进入源代码中:

[java]  view plain copy print ?
  1. /** 
  2.      * <p>Select the mapping used to process theselection path for this request. 
  3.      * If no mapping can be identified, createan error response and return 
  4.      * <code>null</code>.</p> 
  5.      * 
  6.      * @param request The servlet request weare processing 
  7.      * @param response The servlet response weare creating 
  8.      * @param path The portion of the requestURI for selecting a mapping 
  9.      * 
  10.      * @exception IOException if an input/outputerror occurs 
  11.      */  
  12.    protectedActionMapping processMapping(HttpServletRequestrequest,  
  13.                                           HttpServletResponse response,  
  14.                                           String path)  
  15.         throws IOException {  
  16.    
  17.         // Is there a mapping for this path?  
  18.         ActionMapping mapping = (ActionMapping)  
  19.             moduleConfig.findActionConfig(path);  
  20.    
  21.         // If a mapping is found, put it in the request and return it  
  22.         if (mapping != null) {  
  23.             request.setAttribute(Globals.MAPPING_KEY, mapping);  
  24.             return (mapping);  
  25.         }  
  26.    
  27.         // Locate the mapping for unknown paths (if any)  
  28.         ActionConfig configs[] = moduleConfig.findActionConfigs();  
  29.         for (int i = 0; i < configs.length; i++) {  
  30.             if (configs[i].getUnknown()) {  
  31.                 mapping = (ActionMapping)configs[i];  
  32.                 request.setAttribute(Globals.MAPPING_KEY, mapping);  
  33.                 return (mapping);  
  34.             }  
  35.         }  
  36.    
  37.         // No mapping can be found to process this request  
  38.         String msg = getInternal().getMessage("processInvalid");  
  39.         log.error(msg + " " + path);  
  40.         response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);  
  41.          
  42.         return null;  
  43.     }  


    首先我们传入我们在上一步截取的路径,通过moduleConfig的findAction方法来查找ActionConfig,并且返回ActionMapping。具体代码是:

[java]  view plain copy print ?
  1. ActionMapping mapping =(ActionMapping)  
  2.        moduleConfig.findActionConfig(path);  


    如果找到,那么就讲ActionMapping存放到request的context中。代码:

[java]  view plain copy print ?
  1. if (mapping != null) {  
  2.             request.setAttribute(Globals.MAPPING_KEY, mapping);  
  3.             return (mapping);  
  4.         }  


    如果没有通过path找到mapping,则在Actionconfig中遍历为未知路径寻找mapping,如果找到则存放到request中,如果没有找到,则返回错误信息,具体代码如下:

[java]  view plain copy print ?
  1. // Locate the mapping for unknownpaths (if any)  
  2.         ActionConfig configs[] = moduleConfig.findActionConfigs();  
  3.         for (int i = 0; i < configs.length; i++) {  
  4.             if (configs[i].getUnknown()) {  
  5.                 mapping = (ActionMapping)configs[i];  
  6.                 request.setAttribute(Globals.MAPPING_KEY, mapping);  
  7.                 return (mapping);  
  8.             }  
  9.         }  
  10.    
  11.         // No mapping can be found to process this request  
  12.         String msg = getInternal().getMessage("processInvalid");  
  13.         log.error(msg + " " + path);  
  14.         response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);  
  15.          
  16.         return null;  

    通过这一段代码我们就能通过截取的字符串来找到匹配的ActionMapping,并且为下面获取ActionForm做铺

垫。这篇博客主要是让读者明白,Struts1是如何生成ActionMapping和如何获得ActionMapping的,下一篇博客要介绍如何获得ActionForm。敬请期待

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值