I have a struts application where certain actions need a log entry made in a database. I figured the best way to accomplish this was an interface for my actions to implement, and an interceptor to do the logging.
So, here's my interface:
public interface LoggableAction
{
public String getUser();
public String getTransactionType();
public String getValue();
}Now, I have my actions implement this interface, and have my interceptor do:
public class OperatorHistoryInterceptor extends AbstractInterceptor
{
public String intercept(ActionInvocation invocation) throws Exception
{
Object action = invocation.getAction();
if (action instanceof LoggableAction)
{
// Log to database
}
return invocation.invoke();
}
}<interceptors> <interceptor name="operatorHistory" class="OperatorHistoryInterceptor" /> <interceptor-stack name="validationWorkflowStack"> <interceptor-ref name="prepare"/> <interceptor-ref name="basicStack"/> <interceptor-ref name="validation"/> <interceptor-ref name="operatorHistory"/> <interceptor-ref name="passwordExpired"/> <interceptor-ref name="workflow"/> </interceptor-stack> </interceptors>
That's it, just extend the interface with your actions, and off you go.