Thursday, March 1, 2012

IncludeAction in Struts

This is an Action that includes the context-relative URI specified by the parameter property action. 

IncludeAction is much like ForwardAction except that the resulting resource is included in the 
HTTP response instead of being forwarded to. 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. 
Now the same thing we will change in Struts, to pretend that the above resource is of Struts like below:

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

<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.

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 
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. 
So the output of the "LegacyServletA" is displayed in the same HTML as that of the Struts application. 

No comments:

Post a Comment