ホームページ  >  記事  >  Java  >  Struts1 URL インターセプトの詳細な紹介

Struts1 URL インターセプトの詳細な紹介

黄舟
黄舟オリジナル
2018-05-24 15:19:261667ブラウズ

この記事では主に Struts1 の URL インターセプトを紹介します_Power Node Java Academy によってまとめられた関連情報が必要な場合は、

Struts1 の URL インターセプト

を参照してください。まず、ActionServlet の詳細な分析を行ってみましょう。ブレークポイント デバッグを使用して、基礎となるソース コードを確認します。このインスタンスはポスト モードで送信されるため、ブレークポイントを doPost メソッドに設定します。


実行中のプログラムをデバッグし、doPost にメソッドを入力します。

このメソッドは非常に重要であり、ActionServlet 実行の中核となるメソッドです。

次のメソッドを入力します:


次に、入力を続けます:



このようなメソッドが、文字列をインターセプトするメソッドである processPath メソッドであることを突然発見しました。このメソッドのソース コードは次のとおりです:

/** 
  * <p>Identify and return the path component(from the request URI) that 
  * we will use to select an <code>ActionMapping</code> with which todispatch. 
  * If no such path can be identified,create an error response and return 
  * <code>null</code>.</p> 
  * 
  * @param request The servlet request weare processing 
  * @param response The servlet response weare creating 
  * 
  * @exception IOException if an input/outputerror occurs 
  */ 
 protectedString processPath(HttpServletRequest request, 
        HttpServletResponse response) 
  throws IOException { 
 
  String path = null; 
 
  // For prefix matching, match on the path info (if any) 
  path = (String) request.getAttribute(INCLUDE_PATH_INFO); 
  if (path == null) { 
   path = request.getPathInfo(); 
  } 
  if ((path != null) && (path.length() > 0)) { 
   return (path); 
  } 
 
  // For extension matching, strip the module prefix and extension 
  path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); 
  if (path == null) { 
   path = request.getServletPath(); 
  } 
  String prefix = moduleConfig.getPrefix(); 
  if (!path.startsWith(prefix)) { 
   String msg =getInternal().getMessage("processPath"); 
    
   log.error(msg + " " + request.getRequestURI()); 
   response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 
 
   return null; 
  } 
   
  path = path.substring(prefix.length()); 
  int slash = path.lastIndexOf("/"); 
  int period = path.lastIndexOf("."); 
  if ((period >= 0) && (period >slash)) { 
   path = path.substring(0, period); 
  } 
  return (path); 
 
}

このコードを分析します:

path = (String)request.getAttribute(INCLUDE_PATH_INFO); 
  if (path == null) { 
   path = request.getPathInfo(); 
  } 
  if ((path != null) && (path.length() > 0)) { 
   return (path); 
  }

このコードでは、まずパス情報が javax.servlet.include.path_info に存在するかどうかを判断します。ここでは、ページが RequestDispatcher に表示されていることを知る必要があります。 .include モードの場合、この属性値は存在します。したがって、ここに値がない場合は、path=request.getPathInfo() プログラムに入ります。ここで getPathInfo によって取得される値は、サーブレットに対する相対パス情報です。

// For extension matching, stripthe module prefix and extension 
  path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); 
  if (path == null) { 
   path = request.getServletPath(); 
  } 
  String prefix = moduleConfig.getPrefix(); 
  if (!path.startsWith(prefix)) { 
   String msg =getInternal().getMessage("processPath"); 
    
   log.error(msg + " " + request.getRequestURI()); 
   response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 
 
   return null; 
  }

このコードは、javax.servlet.include.servlet_path に値があるかどうかを判断するためのものです。この属性値は、ページが requestDispatcher.include モードで表示される場合にのみ存在するため、ここには値はありません。次に、 path = request.getServletPath(); と入力します。このメソッドは、リクエスト URI コンテキストを返した後に部分文字列を取得するため、ここでの戻り値は「/」とアクセス ページの名前とサフィックス (これは私の mvc インスタンスと同じですか)インターセプトの原則?次に、次のコードを入力します:

path = path.substring(prefix.length()); 
  intslash = path.lastIndexOf("/"); 
  intperiod = path.lastIndexOf("."); 
  if((period >= 0) && (period > slash)) { 
   path = path.substring(0, period); 
  } 
  return (path);

ここでの方法は主に上記の方法と同じですが、主なことはサフィックスを削除することです。

以上がStruts1 URL インターセプトの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。