アクションクラスのexecuteメソッドの前後に、ロギングと言う何かをしたいです。私はストラット 1.3 を使用しています。これはアスペクトプログラミングに似ています。
RequestProcessor の processPreprocess() をオーバーライドしてみましたが、それは execute() の前にのみ呼び出されます。さらに、両方の場所(前と後)で ActionMapping にアクセスしたい。
どうすればこれを達成できますか?
あなたの要件を達成するためにフィルターを試してみるべきだと思います。以下のコードのように 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 ;
}