欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別

    Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別

    這篇文章主要給大家介紹了關(guān)于Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • maven依賴沖突加載順序與解決

    maven依賴沖突加載順序與解決

    這篇文章主要介紹了項(xiàng)目中同時(shí)引用了相同依賴的不同版本,也就是沖突,maven?是如何選擇的,了解了有助于解決項(xiàng)目中的依賴問題,需要的朋友可以參考下
    2024-01-01
  • JAVA中AES對(duì)稱加密和解密過程

    JAVA中AES對(duì)稱加密和解密過程

    這篇文章主要為大家詳細(xì)介紹了JAVA中AES對(duì)稱加密和解密過程,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Mybatis返回單個(gè)實(shí)體或者返回List的實(shí)現(xiàn)

    Mybatis返回單個(gè)實(shí)體或者返回List的實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis返回單個(gè)實(shí)體或者返回List的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java多線程yield心得分享

    Java多線程yield心得分享

    前幾天復(fù)習(xí)了一下多線程,發(fā)現(xiàn)有許多網(wǎng)上講的都很抽象,所以,自己把網(wǎng)上的一些案例總結(jié)了一下
    2013-12-12
  • Spring框架實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能的代碼示例

    Spring框架實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能的代碼示例

    之前項(xiàng)目需要在驗(yàn)證碼模塊,增加滑動(dòng)驗(yàn)證碼,用來給手機(jī)端使用的,大概看了下,主要方法就是將圖片切割,然后記住偏移量,進(jìn)行滑動(dòng),所以本文給大家介紹了Spring框架實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能的方法示例,需要的朋友可以參考下
    2024-07-07
  • IDEA 2019.1.3 激活碼大全

    IDEA 2019.1.3 激活碼大全

    本文是小編給大家收藏整理的IDEA 2019.1.3 激活碼大全,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-10-10
  • IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)

    IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)

    這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • 教你怎么使用hadoop來提取文件中的指定內(nèi)容

    教你怎么使用hadoop來提取文件中的指定內(nèi)容

    發(fā)現(xiàn)有很多小伙伴不會(huì)使用hadoop來提取文件中的指定內(nèi)容,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼示例,對(duì)不會(huì)這個(gè)方法的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • java自定義日期轉(zhuǎn)化類示例

    java自定義日期轉(zhuǎn)化類示例

    這篇文章主要介紹了java自定義日期轉(zhuǎn)化類示例,需要的朋友可以參考下
    2014-05-05

最新評(píng)論