Rule Definition
It is a good practice to extend the struts Action class, and all specific Actions should inherit from it. With such inheritance tree, the basic action class (those inheriting from Struts Action class) implements the methods shared by all the Action class to avoid code redundancy.
Remediation
Have Action Class inherit from one single extension of Struts Action Class that is abstract or have an abstract method.
Violation Code Sample
Public class MyAction extends Action { // VIOLATION
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Some code
}
}
Fixed Code Sample
Public class MyBaseAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//Call all common methods
commonTasks();
return executeSpecificTask(mapping,form,
request,response);
}
private void commonTasks() {
//Code for common tasks
}
//Provide implementation of this method in sub-classes
public abstract ActionForward executeSpecificTask (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
Public class MyAction extends MyBaseAction { // FIXED
public ActionForward executeSpecificTask (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// some code
}
}
Reference
http://www.jaxmag.com/itr/online_artikel/psecom,id,648,nodeid,147.html
http://www.sitepoint.com/article/struts-first-steps/2
Related Technologies
JEE
Technical Criterion
Architecture - Reuse
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.