CRITICAL
Rule Definition
- The Action classes are almost totally independent of the names of the presentation pages. The pages can be renamed (for example) during a redesign, with negligible impact on the Action classes themselves. If the names of the "next" pages were hard coded into the Action classes, all of these classes would also need to be modified.
- If Struts tags are used, then if a reference to a JSP page is directly made, the behaviour of the Struts tags may become unpredictable. This because the ActionServlet or FilterDispatcher in Struts intercepts the request and provides appropriate attributes such as Message Resource Bundles and also FormBeans.
Remediation
Use the action mapping table in struts configuration file.
Violation Code Sample
public class TestAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ActionForward("/myPage.jsp"); // VIOLATION
}
}
Fixed Code Sample
----> TestAction.java:
public class TestAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("mypage"); // FIXED
}
}
----> struts-config.xml:
<action
path="/mypage" type="com.cast.TestAction">
<forward name="mypage" path="/pages/myPage.jsp"/>
</action>
Related Technologies
JEE
Technical Criterion
Architecture - Multi-Layers and Data Access
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.