Struts comes packaged with several built-in utility actions that provide functionality that is useful to many applications. Table 5-2 lists each of the built-in actions and their purpose.
| Action | Description |
| DispatchAction | Provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. |
| DownloadAction | Provides a mechanism for easing the creation of file download actions. |
| EventDispatchAction | Provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. |
| ForwardAction | Provides a mechanism for forwarding to a specified URL. |
| IncludeAction | Provides a mechanism for including the contents of a specified URL. |
| LocaleAction | Provides a mechanism for setting a user's locale and then forwarding to a specified page. |
| LookupDispatchAction | Provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. |
| MappingDispatchAction | Provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. |
| SwitchAction | Provides a mechanism for switching between modules in a modularized Struts application. |
The following sections describe each of the built-in actions in detail.
The org.apache.struts.actions.DispatchAction class provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. For example, consider a set of related functions for adding a user, updating a user, and removing a user. Instead of creating an AddUserAction class, an UpdateUserAction class, and a RemoveUserAction class, by extending DispatchAction, you can create one UserAction class that has three methods: add( ), update( ), and remove( ). At run time, DispatchAction manages routing requests to the appropriate method in its subclass. DispatchAction determines which method to call based on the value of a request parameter that is passed to it from the incoming request.
To use DispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. Additionally, you have to set up for the action an action mapping that specifies the name of a request parameter that will be used to select which method should be called for each request. Following is an example UserAction class that extends DispatchAction:
package com.jamesholmes.minihr;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class UserAction extends DispatchAction
{
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Add user.
...
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Update user.
...
return mapping.findForward("success");
}
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Remove user.
...
return mapping.findForward("success");
}
}
Notice that this class does not provide an implementation for the execute( ) method the way typical Action classes do. This is because DispatchAction provides to you an implementation of this method that manages delegating to the individual methods. In order for your DispatchAction subclass to work, you must create in the Struts configuration file an action mapping that specifies the name of a request parameter that will be used to select the method that will be called for specific requests. Following is a sample snippet that illustrates how to do this:
<action-mappings>
<action path="/User" type="com.jamesholmes.minihr.UserAction" parameter="function"/>
</action-mappings>
The value specified with the parameter attribute of the action tag will be used as the name of a request parameter that will contain the name of a method to invoke for handling the request. Given the preceding mapping of /User to UserAction, the following URL will invoke the add( ) method (assuming the application was run on your localhost at port 8080 and the application was deployed as /MiniHR/):
http://localhost:8080/MiniHR/User.do?function=add
To invoke the remove( ) method, use the following URL:
The org.apache.struts.actions.DownloadAction class provides a mechanism for easing the creation of file download actions. Instead of creating an action from scratch with all of the required infrastructure code for downloading files, the DownloadAction class can be extended. DownloadAction provides all of the infrastructure code in a simple, reusable package. DownloadAction subclasses must include a getStreamInfo( ) method that returns a StreamInfo instance with details about the file to download.
package com.jamesholmes.minihr;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class ReportDownloadAction extends DownloadAction
{
protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
String contentType = "application/vnd.ms-excel";
File file = new File("/dir1/dir2/report.xls");
return new FileStreamInfo(contentType, file);
}
}
When the DownloadAction subclass, such as ReportDownloadAction in the preceding example, is invoked, it will stream the file specified in the getStreamInfo( ) method back to the browser.
The org.apache.struts.actions.EventDispatchAction class is a subclass of DispatchAction and provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. For example, consider a set of related functions for adding a user, updating a user, and removing a user. Instead of creating an AddUserAction class, an UpdateUserAction class, and a RemoveUserAction class, by extending EventDispatchAction, you can create one UserAction class that has three methods: add( ), update( ), and remove( ). At run time, EventDispatchAction manages routing requests to the appropriate method in its subclass. EventDispatchAction determines which method to call based on the presence of a request parameter that is passed to it from the incoming request.
To use EventDispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. Additionally, you have to set up for the action an action mapping that specifies the names of request parameters that will be used to select which method should be called for each request. Following is an example UserAction class that extends EventDispatchAction:
package com.jamesholmes.minihr;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.EventDispatchAction;
public class UserAction extends EventDispatchAction
{
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Add user.
...
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Update user.
...
return mapping.findForward("success");
}
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Remove user.
...
return mapping.findForward("success");
}
Notice that this class does not provide an implementation for the execute( ) method the way typical Action classes do. This is because EventDispatchAction provides to you an implementation of this method that manages delegating to the individual methods. In order for your EventDispatchAction subclass to work, you must create in the Struts configuration file an action mapping that specifies the names of request parameters that will be used to select the method that will be called for specific requests. Following is a sample snippet that illustrates how to do this:
<action-mappings>
<action path="/User" type="com.jamesholmes.minihr.UserAction"parameter="add,update,delete=remove,default=add"/>
</action-mappings>
The value specified with the parameter attribute of the action tag specifies a comma-delimited list of method names to dispatch to if a request parameter of the same name is present in the request. In the preceding example, the add( ) method will be invoked for handling the request if an "add" request parameter is sent to the action. The value of the "add" request parameter is disregarded; only the presence of the request parameter is needed. The remove( ) method will be invoked if a request parameter named "delete" is sent. The remove( ) method, instead of a delete( ) method, is invoked because of an alias of "delete". Method aliases are specified using alias=method notation, where alias is the name of the alias for a method and method is the name of the method to invoke. A default method can be specified by using an alias with the name of "default" as shown in the preceding example. Default aliases are used for scenarios where no request parameter matches the method names specified with the action tag's parameter attribute.
Given the preceding mapping of /User to UserAction, the following URL will invoke the add( ) method (assuming the application was run on your localhost at port 8080 and the application was deployed as /MiniHR/):
http://localhost:8080/MiniHR/User.do?add.x=103
To invoke the remove( ) method, use the following URL:
http://localhost:8080/MiniHR/User.do?remove=true
The org.apache.struts.actions.ForwardAction class provides a mechanism for forwarding to a specified URL. As explained earlier, in an MVC Web application, all requests to the application are supposed to flow through the Controller servlet. This ensures that the Controller layer of the application has an opportunity to prepare any resources that may be needed to handle the request (i.e., selecting the correct module and so on). ForwardAction is provided as a simple utility action that can be used for scenarios in which you simply want to link to a JSP page. Of course, linking directly to the JSP would be a violation of the MVC principles because all requests are supposed to be routed through the Controller. ForwardAction can be used to create links to JSPs so that you don't have to create an action whose only responsibility is to forward a request every time you want to link to a JSP. With ForwardAction, you simply create an action mapping in the Struts configuration file and specify the location to which the action will forward.
To use ForwardAction, simply create action mapping entries in the Struts configuration file, as shown next:
<action-mappings>
<action path="/menu"
type="org.apache.struts.actions.ForwardAction"
parameter="/menu.jsp/>
</action-mappings>
For each page to which you want to link, you must create an action mapping. Each action mapping uses ForwardAction, but each specifies a different path for the action. The parameter attribute specifies the URL that will be forwarded to when the specified path is accessed.
An alternative solution to using ForwardAction is to use the forward attribute of the action tag in the Struts configuration file, as shown here:
<action-mappings>
<action path="/menu" forward="/menu.jsp"/>
</action-mappings>
These two approaches effectively yield the same results.
The org.apache.struts.actions.IncludeAction class provides a mechanism for including the contents of a specified URL. This action behaves similarly to ForwardAction, but instead of forwarding to the specified URL, the specified URL is included. This action is useful when you want to include the contents of one page in another.
Using IncludeAction is quite easy. Just create action mapping entries in the Struts configuration file:
<action-mappings>
<action path="/menu" type="org.apache.struts.actions.IncludeAction" parameter="/menu.jsp/>
</action-mappings>
For each page you want to include, you must create an action mapping. Each action mapping uses IncludeAction, but specifies a different path for the action. The parameter attribute specifies the URL that will be included when the specified path is accessed.
An alternative solution to using IncludeAction is to use the include attribute of the action tag in the Struts configuration file, as shown here:
<action-mappings>
<action path="/menu"
include="/menu.jsp"/>
</action-mappings>
These two approaches effectively yield the same results.
The org.apache.struts.actions.LocaleAction class provides a mechanism for setting a user's locale and then forwarding to a specified page. This action provides a convenient mechanism for changing a user's locale. For example, consider a site that is offered in English and Spanish versions. LocaleAction can be used to offer users a way to switch between the two languages without having to change their browser settings. With LocaleAction you simply create an action mapping and then link to the action, specifying request parameters for which locale to switch to and a page to forward after the locale has been switched.
To use LocaleAction, you must create an action mapping entry for it in the Struts configuration file and then link to the action, specifying locale information and a page to forward to after the locale has been set.
Following is an example of how to configure LocaleAction in the Struts configuration file:
<action-mappings>
<action path="/SwitchLocale" type="org.apache.struts.actions.LocaleAction"/>
</action-mappings>
Once configured in the Struts configuration file, LocaleAction can be put to use. Simply create a link to the action and specify the locale settings that will be set and a page to forward to. Locale settings are specified with two request parameters: language and country. The page to forward to after setting the locale is specified with the page request parameter. The following URL illustrates how to use the request parameters:
http://localhost:8080/MiniHR/SwitchLocale.do?country=MX&language=es&page=/Menu.do
This example URL sets the country to MX (Mexico) and the language to es (Spanish). The /Menu.do page will be forwarded to after the new locale has been set.
The org.apache.struts.actions.LookupDispatchAction class is a subclass of DispatchAction and provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function. For example, consider a set of related functions for adding a user, updating a user, and removing a user. Instead of creating an AddUserAction class, an UpdateUserAction class, and a RemoveUserAction class, by extending LookupDispatchAction, you can create one UserAction class that has three methods: add( ), update( ), and remove( ).
At run time, LookupDispatchAction manages routing requests to the appropriate method in its subclass. LookupDispatchAction determines which method to route to based on the value of a request parameter being passed to it from the incoming request. LookupDispatchAction uses the value of the request parameter to reverse-map to a property in the Struts resource bundle file (e.g., MessageResources.properties). That is, the value of the request parameter is compared against the values of properties in the resource bundle until a match is found. The key for the matching property is then used as a key to another map that maps to a method in your LookupDispatchAction subclass that will be executed.
To use LookupDispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. The subclass must also include a getKeyMethodMap( ) method that maps methods in the class to keys in the Struts resource bundle file. Additionally, you have to set up for the action an action mapping that specifies the name of a request parameter that will be used to select which method will be called for each request. Following is an example UserAction class that extends LookupDispatchAction:
package com.jamesholmes.minihr;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
public class UserAction extends LookupDispatchAction
{
protected Map getKeyMethodMap()
{
HashMap map = new HashMap();
map.put("button.add", "add");
map.put("button.update", "update");
map.put("button.remove", "remove");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Add user.
...
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Update user.
...
return mapping.finwForward("success");
}
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Remove user.
...
return mapping.findForward("success");
}
Notice that this class does not provide an implementation for the execute( ) method as other Action classes do. This is because LookupDispatchAction provides to you an implementation of this method that manages delegating to the individual methods. Notice also the implementation of the getKeyMethodMap( ) method. This method is required by LookupDispatchAction subclasses and is used to map the names of keys in the Struts resource bundle file to methods in the class. The keys' values in the bundle file are used to match against the value of the incoming request parameter specified by the parameter attribute of the action tag in the Struts configuration file.
In order for your LookupDispatchAction subclass to work, you must create in the Struts configuration file an action mapping that specifies the name of a request parameter that will be used to select the method that will be called for a specific request.
Following is a sample snippet that illustrates how to do this:
<action-mappings>
<action path="/User" type="com.jamesholmes.minihr.UserAction"
parameter="function"/>
</action-mappings>
The value specified with the parameter attribute of the action tag will be used as the name of a request parameter that will contain the value of a key in the Struts resource bundle shown here:
button.add=Add User
button.update=Update User
button.remove=Remove User
LookupDispatchAction will use the value of the incoming request parameter to perform a reverse lookup for a key in the resource bundle. The matching key is then mapped to the appropriate method to execute based on the key-to-method mapping specified by the getKeyMethodMap( ) method.
Given the preceding mapping of /User to UserAction, the following URL will invoke the add( ) method (assuming the application was run on your localhost at port 8080 and the application was deployed as /MiniHR/):
http://localhost:8080/MiniHR/User.do?function=Add%20User
To invoke the remove( ) method, use the following URL:
http://localhost:8080/MiniHR/User.do?function=Remove%20User
The org.apache.struts.actions.MappingDispatchAction class is a subclass of DispatchAction and provides a mechanism for modularizing a set of related functions into a single action, eliminating the need to create separate, independent actions for each function. For example, consider a set of related functions for adding a user, updating a user, and removing a user. Instead of creating an AddUserAction class, an UpdateUserAction class, and a RemoveUserAction class, by extending MappingDispatch Action, you can create one UserAction class that has three methods: add( ), update( ), and remove( ). At run time, MappingDispatchAction manages routing requests to the appropriate method in its subclass.
To use MappingDispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. Additionally, you must set up action mappings that specify which method will be called for each request.
Following is an example UserAction class that extends MappingDispatchAction:
package com.jamesholmes.minihr;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
public class UserAction extends MappingDispatchAction
{
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Add user.
...
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Update user.
...
return mapping.findForward("success");
}
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Remove user.
...
return mapping.findForward("success");
}
Notice that this class does not provide an implementation for the execute( ) method as other Action classes do. This is because MappingDispatchAction provides to you an implementation of this method that manages delegating to the individual function methods.
In order for your MappingDispatchAction subclass to work, you must create in the Struts configuration file action mappings that specify the method that will be called for specific requests. Following is a sample snippet that illustrates how to do this:
<action-mappings>
<action path="/AddUser"
type="com.jamesholmes.minihr.UserAction"
parameter="add"/>
<action path="/UpdateUser"
type="com.jamesholmes.minihr.UserAction"
parameter="update"/>
<action path="/RemoveUser"
type="com.jamesholmes.minihr.UserAction"
parameter="remove"/>
</action-mappings>
Notice that each action mapping uses the UserAction class, but specifies a different path for the action. Each of the unique paths will be processed by the same action, but a different method will be called based on the value specified with the parameter attribute. The value specified with the parameter attribute must match the name of a method in your MappingDispatchAction subclass.
The org.apache.struts.actions.SwitchAction class provides a mechanism for switching between modules in a modularized Struts application. Struts enables you to modularize your Struts application. Each module has its own set of configuration data as well as its own request processor. SwitchAction works similarly to ForwardAction, except that before forwarding to a specified resource, the action changes the currently selected module. This is useful for forwarding to JSPs outside of the current module.
| |
<action-mappings>
<action path="/SwitchModule" type="org.apache.struts.actions.SwitchAction"/>
</action-mappings>