1。struts2のインターセプター(フレーム関数コア)
1。フィルターとインターセプター
フィルター対インターセプター機能は一つのことです。フィルターは、リクエストと応答をフィルタリングできるサーブレット仕様のテクノロジーです。
インターセプターは、Struts2フレームワークのテクノロジーであり、AOP(セクション指向)プログラミングのアイデアを実装します。これは、プラグ可能であり、特定のアクションメソッドにアクセスする前または後に傍受できます。
インターセプタースタック:特定の順序でインターセプターをチェーンに結合します。インターセプターメソッドにアクセスすると、Struts2インターセプターチェーンのインターセプターは、以前に定義した順序で順番に呼び出されます。
Struts2実行原理 - 基礎分析
2。カスタムインターセプター
Struts2は、インターセプターインターフェイスインターセプターインターフェイスを定義します。
インターセプターインターフェイスには3つの抽象的なメソッドがあります
•init:この方法は、インターセプターが作成された直後に呼び出され、インターセプターの寿命の間に1回のみ呼び出されます。関連するリソースは、この方法で初期化できます。
•lecept:このメソッドは、アクションリクエストが傍受されるたびに1回呼び出されます。
•破壊:この方法は、インターセプターが破壊される前に呼び出され、インターセプターのライフサイクル中に1回のみ呼び出されます。
Strutsは、アクションのためにプログラマーが登録した各インターセプターのインターセプトメソッドを順番に呼び出します。インターセプトメソッドが呼び出されるたびに、StrutsはActionInvocationインターフェイスのインスタンスを通過します。
ActionInvocation:特定のアクションの実行ステータスを表します。インターセプターは、このクラスのオブジェクトからアクションに関連付けられたアクションオブジェクトと結果オブジェクトを取得できます。インターセプター独自のタスクを完了した後、インターセプターは、アクション処理プロセスの次のステップに、アクションインボージーオブジェクトのInvokeメソッドを呼び出します。
また、AddPreresultListener actionInvocationオブジェクトのAddPreresultListenerメソッドを呼び出して、1つ以上のPreresultListenerリスナーを「ハング」することもできます。このリスナーオブジェクトは、アクションが実行された後、アクション結果の実行を開始する前に何かを行うことができます。
カスタムインターセプターステップ:
a。 com.opensymphony.xwork2.interceptor.interceptorインターフェイスを実装するクラスを作成するか、継承
com.opensymphony.xwork2.interceptor.abstractInterceptorクラス。 (アダプターモード)、通常、AbstractInterceptorを継承することを選択します(インターセプターはメモリに存在します)。 AbstractInterceptorクラスがインターセプターインターフェイスを実装するためです。 INITとDESTUREのための空白の実装を提供します
2つのインターセプターInterceptordemo1とInterceptordemo2を書き込みます
パッケージcom.itheima.interceptor; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.interceptor.abstractInterceptor; public class interceptordemo1は抽象インターセプターを拡張します。 string rtvalue = invocation.invoke(); //リリース、Stringがここに戻るのはなぜですか?最終結果がアクションの結果を返すため、アクションの結果は文字列型system.out.println( "intercept demo1")です。 rtvalueを返します。 }}パッケージcom.itheima.interceptor;インポートcom.opensymphony.xwork2.actioninvocation; Import com.opensymphony.xwork2.interceptor.abstractInterceptor; Import com.opensymphony.xwork2.interceptor.preresultlistener; Interception(ActionInvocation Invocation)スロー例外{// Invocation.AddpreresultListener(new PreresultListener(){// // public void beforeresult(actioninvocation invocation、string resultcode){// system.out.println( "results"); //} //}); System.out.println( "インターセプトDEMO2の前"); string rtvalue = vocation.invoke(); // release system.out.println( "intercepted demo2"); rtvalueを返します。 }}b。 struts.xmlで定義し、インターセプターを定義し、使用する前に最初に定義する必要があります。
<パッケージ名= "P1" extends = "struts-default"> <! - 定義インターセプター:現在のパッケージに対してのみ有効 - > <インターセプター名= "interceprotdemo1"> </interceptor> <interceptor> <interceprotdemo2 "> </interceptor> </interpector>
c。アクション構成で使用できます
<アクションname = "action1" method = "execute"> <! - 定義されたインターセプターを使用します。インターセプターが指定されていない場合、デフォルトのスタックのすべてのインターセプターがデフォルトで使用されます。インターセプターが指定されると、デフォルトは無効です - > <interceptor-ref name = "interceprotdemo1"> </interceptor-ref> <interceptor-ref name = "interceprotdemo2"> <
アクションクラスDemo1actionを実装します
パッケージcom.itheima.action; import com.opensymphony.xwork2.actionsupport; public class demo1action extends actionsupport {@override public string execute()throws {system.out.println( "execute execute");成功を返す; }}実行結果
ファイルアップロード、データ検証、struts2のアクションへの要求パラメーターをカプセル化するなどの関数は、システムのデフォルトのデフォルトストックのインターセプターによって実装されるため、システムのデフォルトのデフォルトスタックを参照する必要があるインターセプターは、アプリケーションがStruts2フレームワークによって提供される多くの機能を使用できるようにするためです。
インターセプターが指定されていない場合、デフォルトのスタックのすべてのインターセプターがデフォルトで使用されます。インターセプターが指定されると、デフォルトは無効です。カスタムインターセプターの使用に加えて、DefaultStackも使用する必要があります。これを行うことができます
方法1 :(自分で使用)、動作中のCustomとDefaultStackを構成するだけです。
方法2 :(全員がそれを使用する場合)、パッケージのすべてのアクションがカスタムインターセプターを使用したい場合は、インターセプタースタックインターセプタースタックを使用し、インターセプタースタックを定義し、アクションでインターセプターを<デフォルトインターセプターとして定義することができます。
<インターセプター> <インターセプター名= "interceprotdemo1"> </interceptor> <インターセプター名= "interceprotdemo2"> </interceptor> <interceptor-stack name = "mydefaultStack"> <interceptor-ref name = "defaultStack"> </interpector-ref> <interpector-refRef name = "interceprotdemo1"> </interceptor-ref> <interceptor-ref name = "interceprotdemo2"> </interceptor-ref> </interceptor-stack> </interceptors> <action name = "action3" method = "login"> <interceptor-ref = "mydefaultStack"> <sults> /success.jsp </result> </action>
3。Struts2自身のインターセプター
ケース1:ユーザーがログインしているかどうかを確認します
1.ページlogin.jspを書き込みます
<body> <form action = "$ {pagecontext.request.contextpath}/login.action" method = "post"> <input type = "text" name "/> <br/> <入力タイプ=" text "name =" password "/> <br/> <input type =" "submin"2。ログイン検証のためにLoginCheckInterceptorクラスを書き込みます
パッケージcom.itheima.interceptor; Import javax.servlet.http.httpsession; Import org.apache.struts2.servletactionContext; Import com.pensymphony.xwork2.actionvococation; Import com.opensymphony.xwork2.interceptor.abgrigncectorceptor; abgincectorceptor; Intercept(actionInvocation Invocation)スロー例外{httpsession session = servletactionContext.getRequest()。 if(user == null){// login return "login"; //論理ビューに戻る} return rick invocation.invoke(); // release}}}3.構成ファイルstruts.xmlを書き込みます
<パッケージ名= "p2" extends = "struts-default"> <interceptors> <インターセプター名= "logincheckinterceptor"> </interceptor> <interceptor-stack name = "mydefaultStack"> <interceptor-ref name = "defaultStack"> </interceptor-ref name </interceptor-ref> </interceptor-stack> </interceptor-stack> </interceptors> <action name = "login" method = "login"> <result> /login.jsp </result> </action> </package>
4。アクションクラスの顧客アクションを書きます
パッケージcom.itheima.action; import org.apache.struts2.servletactionContext; Import com.opensymphony.xwork2.actionsupport; public class customeractionはactionsupport {public.out.println( "login"); servletactionContext.getRequest()。getSession()。setAttribute( "user"、 "ppp");成功を返す; }}ケース2:アクションメソッドの実行効率の監視
TimerInterceptorを書き込みます
パッケージcom.itheima.interceptor; Import com.opensymphony.xwork2.actionInvocation; Import com.opensymphony.xwork2.interceptor.abstractInterceptor; public class timerinterceptor; public string interceptor {public string intercept(actioninvocation vocution vocution)throws excepsion string rtvalue =招待状.invoke(); system.out.println(rtvalue+"実行時間:"+(system.nanotime() - time)+"nanocond"); rtvalueを返します。 }}構成ファイルを書き込みます
<パッケージ名= "p2" extends = "struts-default"> <interceptors> <インターセプター名= "logincheckinterceptor"> </interceptor> <インターセプター名= "TimerInterceptor"> </interceptor> <インターセプタースタック名= "MyDefaultStack"> <Interpctor-ref = "Defictor-ref> <-ref> <-ref> <-ref> name = "logincheckinterceptor"> </interceptor-ref> <interceptor-ref name = "logincheckinterceptor"> </interceptor-ref> <interceptor-ref name = "TimerInterceptor"> </interceptor-ref> </interceptor-stack> </interceptors> <result "> login
上記からわかるように、複数のフィルターを1つのアクションで構成できます。
4。カスタムインターセプター:傍受方法または傍受しない方法を指定できます
インターセプトする方法または傍受することなく方法を指定できます。フィルターを作成するときは、2つのフィールドを持つMethodFilterInterceptorクラスを実装できます。パラメーターを注入することにより、傍受することなくそれらを指定できます。 2つのパラメーターのうち1つのみを使用できます。インターセプトが少ない場合は、includemethodsを使用できます。より多くのインターセプトがある場合は、excludeMethodsを使用できます。
expludemethods = collectionsemptyset(); //それらを除外します
includemethods = collectionsemptyset(); //それらを含めます
ケース:ログイン検証の例を続けます。
1.フィルターLoginCheckEnterceptorを書き込みます
パッケージcom.itheima.interceptor; Import javax.servlet.http.httpsession; Import org.apache.struts2.servletactionContext; Import com.pensymphony.xwork2.actionInvocation; import com.opensymphony.xwork2.interceptor.AbstruationInterceptor; Import; com.opensymphony.xwork2.interceptor.abstractInterceptor;インポートcom.opensymphony.xwork2.interceptor.methodfilterInterceptor;パブリッククラスのlogincheckEnterceptorは、MethodFilterInterceptorを拡張します。 Object user = session.getAttribute( "user"); if(user == null){// login return "login"; //論理ビューに戻る} return rick invocation.invoke(); // release}}}2。構成ファイルを書き込みます
3.アクションクラスの顧客アクションを書きます
パッケージcom.itheima.action; Import org.apache.struts2.servletactionContext; Import com.opensymphony.xwork2.actionsupport; public class customeraction extends actionsupport {public string add(){system.out.println( "call add()" call add() "); return suctus ed() method "); return success;} public string login(){system.out.println(" login "); servletactioncontext.getRequest()。 4。ページを書きます
addcustomer.jsp
<body>顧客を追加</body>
editcustomer.jsp
<body>顧客を変更</body>
login.jsp
<body> <form action = "$ {pagecontext.request.contextpath}/login.action" method = "post"> <input type = "text" name "/> <br/> <入力タイプ=" text "name =" password "/> <br/> <input type =" "submin"success.jsp
<body> oyeah </body>
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。