Struts Action classes


ForwardAction :


This is an Action that forwards to the context-relative URI specified by the parameter property of our associatedActionMapping. This can be used to integrate Struts with other business logic components that are implemented asservlets or JSP pages, but still take advantage of the Struts controller servlet's functionality such as processing of form beans. To configure the use of this Action in our struts-config.xml file, create an entry like this:

					
<action 
	path="/saveSubscription" 
	type="org.apache.struts.actions.ForwardAction" 
	name="subscriptionForm" 
	scope="request" 
	input="/subscription.jsp" 
	parameter="/path to processing servlet" /> 	

This one will forward control to the context-relative URI specified by the parameter attribute. Steps to use ForwardAction:
  • Using the html:link tag with the action attribute, add a link to the JSP page that points to the action.
  • Create an Action Mapping in the Struts configuration file that uses the ForwardAction with the parameter attribute to specify the JSP path.

Let's consider, we have a JSP page that has a direct link to another JSP page:


<html:link page="/example.jsp">Home< /html:link>
						

Now we want to change the html:link tag to link to an action. Because we already have a link, simply change it as follows:


<html:link action="home">Home</html:link>
						

All we did is, removing the page attribute and adding an Action attribute that points to the home action. Now, we have to create the home action mapping. To add an action mapping to the home action that we referenced in ourhtml:link tag, use this code:


<action
	path="/home"
	type="org.apache.struts.actions.ForwardAction"
	parameter="/example.jsp" />	

The ForwardAction uses the parameter attribute so that we can specify the path. Here the parameter is set to/example.jsp, which was what the page attribute of the html:link tag was originally set to. Thus, the parameter attribute shows where the ForwardAction to forward.


Forward Attribute

ForwardActions get used quite a bit-so much so that the Struts configuration file includes support for them. So rather than doing this:


<action
	path="/home"
	type="org.apache.struts.actions.ForwardAction"
	parameter="/ example.jsp"/>	

we can do this:


<action
	path="/home"
	forward="/example.jsp"/>	


IncludeAction :

This is an Action that includes the context-relative URI specified by the parameter property of our associatedActionMapping. This can be used to integrate Struts with other business logic components that are implemented as servlets or JSP pages, but still take advantage of the Struts controller servlet's functionality such as processing of form beans. To configure the use of this Action in our struts-config.xml file, create an entry like this:


<action 
	path="/saveSubscription"
	type="org.apache.struts.actions.IncludeAction" 
	name="subscriptionForm" 
	scope="request"
	input="/subscription.jsp" 
	parameter="/path to processing servlet"> 	

which will include the context-relative URI specified by the parameter attribute. This shorter form uses the include attribute:

					
<action
	path="/legacy"
	include="/legacy/roar"
	input="/form/userForm2.jsp"
	name="userForm"
	parameter="/legacy/roar"
	validate="true"
	scope="request"/>		

The difference between the IncludeAction and the ForwardAction is that we need to use the IncludeAction only if the action is going to be included by another action or JSP. Therefore, if we have code in our JSP that looks like this:


<jsp:include page="/someWebApp/ someModule/someAction.do " />	

The action could not use a ForwardAction because it would forward control to the new action rather than including its output within the output of the JSP-or throw a nasty IllegalStateException if the output buffer was already committed.


 SwitchAction:

This is a standard Action that switches to a new module and then forwards control to a URI within the new module. Valid request parameters for this Action are:
  • page - Module-relative URI (beginning with "/") to which control should be forwarded after switching.
  • prefix - The module prefix (beginning with "/") of the module to which control should be switched. Use a zero-length string for the default module. The appropriate ModuleConfig object will be stored as a request attribute, so any subsequent logic will assume the new module.

Say we have a web application that uses Struts. Struts has two modules: the default module and an admin module, both shown below:


	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>
			org.apache.struts.action.ActionServlet
		</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml </param-value>
		</init-param>
		<init-param>
			<param-name>config/admin</param-name>
			<param-value>/WEB-INF/struts-config-admin.xml </param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>	

The init parameter config/admin defines the admin module. The config init parameter defines the default module. Now, let's say we have an action in the default module that edits users and we want to delegate the display of those users to an action in the admin module. Perform the following steps:
  • First, map a SwitchAction into the default module as shown here:
    
    <action
    	path="/switch"
    	type="org.apache.struts.actions.SwitchAction">
    </action>	
  • Now, we can set up a forward in the action that edits the users as follows:
    
    <action
    	path="/userSubmit"
    	attribute="userForm"
    	input="/form/userForm.jsp"
    	name="userForm"
    	scope="request"
    	type="action.UserAction">
    	<forward name="success" path="/switch.do?page=/listUsers.do&prefix=/admin"/>
    </action>	

This forward passes two request parameters. The page parameter specifies the module relative action. Thesecond parameter specifies the prefix of the module. We don't have to use forwards to use the SwitchAction; any JSP can link to the SwitchAction to move to any module. The listUser.do action is not defined in the default module; it's defined in the admin. The forward in this example forwards to the action at the path /admin/listUsers.do. The listUser.do is defined in /WEB-INF/struts-config-admin.xml, and the userSubmit. do action is defined in /WEB-INF/struts-config.xml.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email