Nils
Nils

Reputation: 806

How to do something before and after action's execute method in Struts 1.3?

I want to do something say logging, before and after action class's execute method. I am using struts 1.3 . This is more like aspect programming.

I tried processPreprocess() of RequestProcessor by overriding it, but that is called before execute() only. Plus I want to access ActionMapping at both the places(before and after).

How can I achieve this ?

Upvotes: 2

Views: 3266

Answers (1)

Rais Alam
Rais Alam

Reputation: 7016

I guess you should try filter to achieve your requirement. Create a Filter do mapping in web.xml and overide doFilter method like below code.

 public void doFilter(ServletRequest request, ServletResponse response,

       FilterChain chain) throws IOException, ServletException {

        beforeMethod(request,response);

        chain.doFilter(request, response);

        afterMethod(request,response);


    }

If filter is not applicable or fit in your requirement try below logic.

  • Create an Action class, say MyAction, by extending org.apache.struts.action.Action.
  • Create all other Action classes in your Web application by extending MyAction.

In MyAction, create a method operate(), as in

public abstract ActionForward operate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
  • In MyAction add one or more generic methods to the application, for example before(), after().

  • If all Action classes must implement this method, make it abstract.

  • If some Action classes will provide a case-specific implementation, declare the method protected and give it a default implementation.

In MyAction override execute methode like below code

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 ;

}

Upvotes: 1

Related Questions