Saturday, January 26, 2013

ForwardAction and IncludeAction in struts


   The org.apache.struts.actions.ForwardAction class enables you to forward request to the specified URL without going through Controller. ForwardAction is an utility class  used in cases where a user simply needs to forward the control to another JSP . Doing this directly violates the MVC principles.  So they have given this option for  action-mapping. Note that we do not  create any action class here. With ForwardAction , simply create an action mapping  in the Struts configuration file  and specify the location where the action should forward the request with the help of "parameter" attribute of action tag.
For example:

In JSP :

<html:link page="/Welcome.do">
           Click here
</html:link> 


In strus-config :

<action-mappings>
<action
  path="/Welcome"
  type="org.apache.struts.actions.ForwardAction"
  parameter="/Welcome.jsp"/>
</action-mappings> 


there is shortcut for above configuration. you can achieve same simply by:


<action path="/Welcome" include="/Welcome.jsp"/>


org.apache.struts.actions.IncludeAction is somewhat like ForwardAction but the resulting resource is included in the HTTP response instead of forwarding the request to. Actually this is of not much use. Its mainly used to integrate old/legacy applications with Struts transparently. More like integrating simple servlet into struts application. 

In Struts-config file:


<action path="/hello" parameter="/helloServlet"   type="org.apache.struts.actions.IncludeAction"> 
</action>

Jsp:


<jsp:include page="/hello.do"></jsp:include>


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


No comments:

Post a Comment