2

アクションクラスのexecuteメソッドの前後に、ロギングと言う何かをしたいです。私はストラット 1.3 を使用しています。これはアスペクトプログラミングに似ています。

RequestProcessor の processPreprocess() をオーバーライドしてみましたが、それは execute() の前にのみ呼び出されます。さらに、両方の場所(前と後)で ActionMapping にアクセスしたい。

どうすればこれを達成できますか?

4

1 に答える 1

1

あなたの要件を達成するためにフィルターを試してみるべきだと思います。以下のコードのように Filter do mapping inweb.xmlを作成し、メソッドをオーバーライドします。doFilter

 public void doFilter(ServletRequest request, ServletResponse response,

       FilterChain chain) throws IOException, ServletException {

        beforeMethod(request,response);

        chain.doFilter(request, response);

        afterMethod(request,response);


    }

フィルターが適用できない場合、または要件に適合しない場合は、以下のロジックを試してください。

  • MyActionを拡張して Action クラス、たとえば を作成しますorg.apache.struts.action.Action
  • Action classesを拡張して、Web アプリケーションで他のすべてを作成しますMyAction

MyActionで、メソッドを作成しoperate()ます。

public abstract ActionForward operate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
  • MyAction で、1 つ以上の汎用メソッドをアプリケーションに追加します (例before(): after().

  • すべての Action クラスがこのメソッドを実装する必要がある場合は、それを にしabstractます。

  • 一部の Action クラスがケース固有の実装を提供する場合は、メソッドを保護されていると宣言し、デフォルトの実装を指定します。

MyActionオーバーライドでは、以下のコードのようにメソッドを実行します

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 {

           before();

           ActionForward forward = operate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

           after();

           return forward ;

}
于 2013-03-07T08:43:26.080 に答える