February 2005
Intermediate to advanced
528 pages
12h 53m
English
You want to implement and enforce a common feature or behavior across
all of your Actions.
Implement an abstract base class that incorporates the behavior in
its execute( ) method. As shown by the class in
Example 6-1, the execute( )
method calls abstract methods that are implemented by subclasses.
Example 6-1. Abstract base action
package com.oreilly.strutsckbk.ch06;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public abstract class BaseAction extends Action {
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { executeBefore( );
// call the abstract method
ActionForward forward = executeAction( mapping, form,
request, response );
executeAfter( );
return forward;
}
protected abstract ActionForward executeAction( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
private void executeBefore( ) {
//Real stuff goes here
}
private void executeAfter( ) {
//Real stuff goes here
}
protected CommonServices getCommonServices( ) {...}
}For some applications, you may want to apply global business rules any time a user accesses the application, submits a form, or ...