IncludeAction in Struts

By: Fazal Viewed: 153242 times    

IncludeAction is much like ForwardAction except that the resulting resource is included in the HTTP response instead of being forwarded to. It is rarely used. Its only significant use is to integrate legacy applications with Struts transparently. Consider a web site that aggregates information from disparate sources - some of which are non-Struts. The JSP for such a web site consists of <jsp:include>s to include different resources. One of such <jsp:include> that might be as follows: <jsp:include page="/xoom/LegacyServletA" />

It is very clear from the value of the page attribute that it is a non-Struts resource. Wouldn't it be better to have a <jsp:include> that pretends as if the resource exists in the current Struts application? It would be ideal if the page include looked as follows:

<jsp:include page="/App1/" />

The / cannot be a ForwardAction because it would perform a HTTP Forward to the above resource instead of including the resource in the HTTP response. Since the HTTP Response OutputStream closes (The J2EE jargon for this is the response has been committed) after HTTP Forward, the servlet container cannot process the rest of the JSP and include its response in the OutputStream. Consequently it throws a IllegalStateException with a message that “Response is already committed". IncludeAction addresses this problem. Instead of forwarding to the specified resource, it includes the resource in the current response. Consequently the output of the LegacyServletA is displayed in the same HTML as that of the Struts application. You have to add the following ActionMapping in the Struts Config file:

<action path="/legacyA"
type="org.apache.struts.actions.IncludeAction" />

The parameter attribute indicates the actual resource that has to be included in the response. As mentioned earlier, the use of IncludeAction is limited to including responses from existing Servlet in the current page. This requires the use of <jsp:include> in the page. If you web application is aggregating response from legacy servlet applications, portlets seems to be the way to go. Portlet API - JSR 168 has been finalized and it is matter of time before you can develop standardized portals aggregating contents from disparate web applications. Tiles framework is the way to go if you are on a short-term project that wants to aggregate information now (From different applications or may be from various Actions in the same Struts application). Tiles provides a robust alternative to the primitive <jsp:include>.

This is an excerpt from the book "Struts Survival Guide". You can get more details from that book.

Most Viewed Articles (in Struts )

Latest Articles (in Struts)

Comment on this tutorial