Struts1 ActionMappingの例の説明

巴扎黑
リリース: 2017-09-06 09:34:42
オリジナル
1596 人が閲覧しました

この記事では主に Struts1 チュートリアルの ActionMapping を紹介します。編集者はそれを参考として共有します。エディターに従って見てみましょう

まず、ブレークポイントは processpath メソッドの外にあります

今日は、ActionMapping を取得する方法を見ていきます。メソッド---プロセスマッピング。 その前に、ActionMapping について簡単に説明します。ソース コードから、最も重要な属性は、主に対応する Struts のパス、type、forwardMap に似ていることがわかります。 config 設定ファイル。これは、この設定ファイルの情報をメモリに保存します。

特定の mvc small インスタンスの 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 のコード (ActionMapping はこの ActionConfig を継承するため、ActionConfig をより直接的に調べます) は次のとおりです:



コードのこれら 2 つの部分から判断すると、最初に書いた小さな mvc サンプルが Struts フレームワークのプロトタイプであることがさらに確認できます。

ActionMapping の内容についていくつか説明したので、ActionMapping についてはある程度理解できたと思います。それでは、システムはどのように ActionMapping を生成し、どのようにして ActionMapping を見つけるのでしょうか?これが今日話したいことのすべてです:

Web で 2 の構成情報を見てみましょう。 struts-config 構成ファイルを読み取ります。そして設定ファイルの情報をActionMappingに入力します。したがって、サーバーを実行すると、struts-config 構成ファイルの情報に対応する ActionMapping がメモリ内にすでに存在します。今日は、processMapping を通じてこの ActionMapping クラスを読み取ります。

ブレークポイントのデバッグに入り、最初に processMapping メソッドにブレークポイントを設定します。


ソース コードを入力します:


/** 
   * <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; 
  }
ログイン後にコピー

まず、前のステップでインターセプトしたパスを渡し、moduleConfig の findAction メソッドで ActionConfig を検索し、ActionMapping を返します。具体的なコードは次のとおりです:



ActionMapping mapping =(ActionMapping) 
   moduleConfig.findActionConfig(path);
ログイン後にコピー

見つかった場合、ActionMapping はリクエストのコンテキストに保存されます。コード:



if (mapping != null) { 
      request.setAttribute(Globals.MAPPING_KEY, mapping); 
      return (mapping); 
    }
ログイン後にコピー

パスでマッピングが見つからない場合は、Actionconfig を走査して不明なパスのマッピングを見つけます。見つかった場合は、リクエストに保存されます。見つからない場合は、エラー メッセージが表示されます。具体的なコードは次のとおりです。



// 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;
ログイン後にコピー

文字列をインターセプトし、その文字列に基づいて ActionMapping を取得するときの processActionForm を見てみましょう (これは最初の 2 つで紹介されています)。記事)、ActionMapping を使用して ActionForm を作成し、その ActionForm をリクエストまたはセッションに入れて管理します。


まず、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 = 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 &#39;" + 
       mapping.getScope() + "&#39; under attribute key &#39;" + 
       mapping.getAttribute() + "&#39;"); 
    } 
    if ("request".equals(mapping.getScope())) { 
      request.setAttribute(mapping.getAttribute(), instance); 
 
    } else { 
 
      HttpSession session =requestgetSession(); 
 
      session.setAttribute(mapping.getAttribute(), instance); 
 
    } 
 
    return (instance); 
 
  
 
}
ログイン後にコピー

このメソッドの一般的なプロセスは次のとおりです: ActionMapping の名前に基づいて ActionForm を検索します。ActionForm が設定されている場合は、リクエストに進みます。またはセッションを検索すると、リクエストまたはセッション内に作成された ActionForm が存在する場合、それが返されます。存在しない場合は、ActionForm の完了パスに従ってリフレクションを使用して作成され、作成された ActionForm がリクエストまたはセッションに配置され、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 = 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 &#39;"+ name + "&#39;"); 
 
      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); 
 
}
ログイン後にコピー

メソッドは、最初に変数名を定義し、インスタンスの LoginForm 文字列である String name = Mapping.getName(); から値を取得します。その後、FormBeanConfig config =moduleConfig.findFormBeanConfig(name); を呼び出すことにより、対応する LoginForm 文字列が対応するオブジェクトに生成されます。


ここで説明したいのは、struts-config 構成ファイルでこのようなラベル情報を構成したということです:


<form-beans> 
 
    <form-bean name="loginForm" type=".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中赋值了

以上がStruts1 ActionMappingの例の説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!