最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Struts1教程之ActionMapping_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

 更新時(shí)間:2017年09月04日 10:37:57   作者:lfsf802  
這篇文章主要介紹了Struts1教程之ActionMapping,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

首先斷點(diǎn)走出了processpath方法,

  

這個(gè)方法是用來截取字符串的,今天我們來看怎樣獲得ActionMapping的方法---processMapping。

在此之前簡(jiǎn)單說一下ActionMapping,它的源代碼中可以看出,其中最重要的屬性和我們的mvc小實(shí)例中的ActionMapping類似,都是有path、type還有forwardMap,主要是對(duì)應(yīng)的struts-config配置文件而來,這個(gè)就是保存這個(gè)配置文件的信息到內(nèi)存中。

具體的mvc小實(shí)例的ActionMapping代碼如下:

package com.cjq.servlet; 
 
import java.util.Map; 
 
public class ActionMapping { 
 
  private String path; 
   
  private Object type; 
   
  private Map forwardMap; 
 
  public String getPath() { 
    return path; 
  } 
 
  public void setPath(String path) { 
    this.path = path; 
  } 
 
  public Object getType() { 
    return type; 
  } 
 
  public void setType(Object type) { 
    this.type = type; 
  } 
 
  public Map getForwardMap() { 
    return forwardMap; 
  } 
 
  public void setForwardMap(Map forwardMap) { 
    this.forwardMap = forwardMap; 
  } 
   
} 

而Struts中的Actionconfig(因?yàn)锳ctionMapping是繼承這個(gè)ActionConfig的,所以我們來看ActionConfig更加直接)的代碼如下:

從這兩部分代碼來看,更加印證了我在開篇寫的mvc小實(shí)例是一個(gè)struts框架的雛形。

講完ActionMapping的一些內(nèi)容后,相信對(duì)ActionMapping有所了解,那么系統(tǒng)是如何生成ActionMapping和如何找到ActionMapping的呢?這就是今天要說的整體:

我們看下web.xml中有一個(gè)<load-on-startup>2</load-on-startup>  配置信息,這個(gè)信息就是說明了但服務(wù)器已啟動(dòng)就動(dòng)態(tài)讀取struts-config配置文件把配置文件的信息put到ActionMapping中。所以當(dāng)我們運(yùn)行服務(wù)器的時(shí)候,我們?cè)趦?nèi)存中已經(jīng)存在對(duì)應(yīng)struts-config配置文件信息對(duì)應(yīng)的ActionMapping。今天就是要通過processMapping讀取這個(gè)ActionMapping類。

進(jìn)入斷點(diǎn)調(diào)試,首先在processMapping方法上設(shè)置斷點(diǎn)。

    

進(jìn)入源代碼中:

/** 
   * <p>Select the mapping used to process theselection path for this request 
   * If no mapping can be identified, createan error response and return 
   * <code>null</code>.</p> 
   * 
   * @param request The servlet request weare processing 
   * @param response The servlet response weare creating 
   * @param path The portion of the requestURI for selecting a mapping 
   * 
   * @exception IOException if an input/outputerror occurs 
   */ 
  protectedActionMapping processMapping(HttpServletRequestrequest, 
                     HttpServletResponse response, 
                     String path) 
    throws IOException { 
  
    // Is there a mapping for this path? 
    ActionMapping mapping = (ActionMapping) 
      moduleConfig.findActionConfig(path); 
  
    // If a mapping is found, put it in the request and return it 
    if (mapping != null) { 
      request.setAttribute(Globals.MAPPING_KEY, mapping); 
      return (mapping); 
    } 
  
    // Locate the mapping for unknown paths (if any) 
    ActionConfig configs[] = moduleConfig.findActionConfigs(); 
    for (int i = 0; i < configs.length; i++) { 
      if (configs[i].getUnknown()) { 
        mapping = (ActionMapping)configs[i]; 
        request.setAttribute(Globals.MAPPING_KEY, mapping); 
        return (mapping); 
      } 
    } 
  
    // No mapping can be found to process this request 
    String msg = getInternal().getMessage("processInvalid"); 
    log.error(msg + " " + path); 
    response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); 
     
    return null; 
  } 

首先我們傳入我們?cè)谏弦徊浇厝〉穆窂剑ㄟ^moduleConfig的findAction方法來查找ActionConfig,并且返回ActionMapping。具體代碼是:

ActionMapping mapping =(ActionMapping) 
   moduleConfig.findActionConfig(path); 

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

if (mapping != null) { 
      request.setAttribute(Globals.MAPPING_KEY, mapping); 
      return (mapping); 
    } 

如果沒有通過path找到mapping,則在Actionconfig中遍歷為未知路徑尋找mapping,如果找到則存放到request中,如果沒有找到,則返回錯(cuò)誤信息,具體代碼如下:

// Locate the mapping for unknownpaths (if any) 
    ActionConfig configs[] = moduleConfigfindActionConfigs(); 
    for (int i = 0; i < configslength; i++) { 
      if (configs[i].getUnknown()) { 
        mapping = (ActionMapping)configs[i]; 
        request.setAttribute(Globals.MAPPING_KEY, mapping); 
        return (mapping); 
      } 
    } 
  
    // No mapping can be found to process this request 
    String msg = getInternal().getMessage("processInvalid"); 
    log.error(msg + " " + path); 
    response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); 
     
    return null; 

來看下ActionServlet中的一個(gè)方法processActionForm,當(dāng)我們?cè)诮厝∽址?,再根?jù)字符串取得ActionMapping(這是前兩篇文章中介紹的)之后,我們就要用利用ActionMapping來創(chuàng)建ActionForm了,并且把ActionForm放到request或session中管理。

先來看具體struts中processActionForm方法的具體實(shí)現(xiàn):

/** 
 
   * <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 = RequestUtilscreateActionForm 
 
      (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 =requestgetSession(); 
 
      session.setAttribute(mapping.getAttribute(), instance); 
 
    } 
 
    return (instance); 
 
  
 
} 

這個(gè)方法的大體流程是:根據(jù)ActionMapping中的name名稱查找ActionForm,如果配置了ActionForm,那么就到request或session中查找,如果在request或session中存在已經(jīng)創(chuàng)建的ActionForm,那么將返回。如果不存在那么會(huì)根據(jù)ActionForm的完成路徑采用反射進(jìn)行創(chuàng)建,再將創(chuàng)建好的ActionForm放到request或session中,之后返回ActionForm。

具體我們可以跟隨斷點(diǎn)調(diào)試來看看這個(gè)方法是如何運(yùn)行的。

先設(shè)置斷點(diǎn),之后進(jìn)入processActionForm方法。

第一個(gè)步驟就是創(chuàng)建ActionForm:

// Create (if necessary) a formbean to use 
 
    ActionForm instance = RequestUtils.createActionForm 
 
      (request, mapping, moduleConfig, servlet); 
 
    if (instance == null) { 
 
      return (null); 
 
    } 

通過調(diào)用RequestUtils.createActionForm的方法把ActionMapping中的ActionForm字符串生成對(duì)象,并且返回。進(jìn)入這段代碼中:

publicstaticActionForm createActionForm( 
 
      HttpServletRequest request, 
 
      ActionMapping mapping, 
 
      ModuleConfig moduleConfig, 
 
      ActionServlet servlet) { 
 
  
 
    // Is there a form bean associated with this mapping? 
 
    String attribute = mappinggetAttribute(); 
 
    if (attribute == null) { 
 
      return (null); 
 
    } 
 
  
 
    // Look up the form bean configuration information to use 
 
    String name = mapping.getName(); 
 
    FormBeanConfig config =moduleConfigfindFormBeanConfig(name); 
 
    if (config == null) { 
 
      log.warn("No FormBeanConfig found under '"+ name + "'"); 
 
      return (null); 
 
    } 
 
  
 
    ActionForm instance = lookupActionForm(request,attribute, mappinggetScope()); 
 
  
 
    // 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); 
 
} 
   

方法首先定義變量name,并且從mapping中獲取值,String name = mapping.getName();也就是我們實(shí)例中的LoginForm字符串。之后通過調(diào)用FormBeanConfig config =moduleConfig.findFormBeanConfig(name);這句話把相應(yīng)的LoginForm字符串生成相應(yīng)的對(duì)象。

這里要說明的是我們?cè)趕truts-config配置文件中,配置過這樣一個(gè)標(biāo)簽信息:

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

這個(gè)標(biāo)簽在服務(wù)器一啟動(dòng)的時(shí)候就會(huì)利用digester讀取這里的配置信息,并且放在FormBeanConfig類中,這樣我們可以通過上面那一句話就可以把LoginForm字符串生成相應(yīng)的對(duì)象。

之后調(diào)用了ActionForm instance = lookupActionForm(request,attribute, mapping.getScope());這個(gè)方法,這個(gè)方法主要是查找scope屬性中有沒有存在ActionForm。具體實(shí)現(xiàn):

if ("request".equals(scope)){ 
 
      instance = (ActionForm)request.getAttribute(attribute); 
 
    } else { 
 
      session = request.getSession(); 
 
      instance = (ActionForm)session.getAttribute(attribute); 
 
    } 

這里判斷scope屬性值是否為request,如果是則從request中讀出ActionForm,如果不是則從session中讀出。程序如果是第一次執(zhí)行,那么ActionForm會(huì)是為空的。因?yàn)檫@里的ActionForm為空,所以就進(jìn)入了if判斷語句中,最后通過調(diào)用return createActionForm(config, servlet);創(chuàng)建ActionForm并且返回。

之后processActionForm就會(huì)把返回來的ActionForm放入request或者session中。具體實(shí)現(xiàn)就是:

if ("request".equals(mapping.getScope())){ 
 
      request.setAttribute(mapping.getAttribute(), instance); 
 
    } else { 
 
      HttpSession session =request.getSession(); 
 
      session.setAttribute(mapping.getAttribute(), instance); 
 
    } 

到此為止,ActionForm就創(chuàng)建完成,當(dāng)ActionForm創(chuàng)建完成之后,就要用其他的方法來往ActionForm中賦值了

相關(guān)文章

最新評(píng)論

巴林左旗| 绥中县| 巴青县| 赫章县| 镇坪县| 玉山县| 嘉祥县| 宾阳县| 札达县| 河西区| 黎川县| 台州市| 昌黎县| 双城市| 武清区| 东安县| 乳山市| 盐边县| 张家港市| 从江县| 民县| 宿迁市| 灵丘县| 泾阳县| 克东县| 关岭| 临夏县| 商洛市| 克什克腾旗| 铁岭县| 康定县| 呼伦贝尔市| 日土县| 丰台区| 苍梧县| 游戏| 富裕县| 晋宁县| 庄河市| 鹰潭市| 南宁市|