| < Day Day Up > |
|
In this chapter, we discuss the org.apache.struts.action.ActionMapping class and how you can extend it to provide specialized mapping information to the ActionServlet. We also present an example ActionMapping extension that will be leveraged in our wroxstruts application.
The goal of this chapter is to show you how custom ActionMappings are created and deployed. Our intention is also to demonstrate how useful an ActionMapping extension can be.
An ActionMapping object describes an Action instance to the ActionServlet. It represents the information that uniquely defines an instance of a particular action class. The values defined by an ActionMapping object are what make a particular <action> definition unique.
The ActionMapping object also provides useful information to the Action.execute() method, giving an Action object the ability to alter its behavior based on the values describing a particular ActionMapping instance. The following code snippet shows the method signature of the Action.execute() method:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
You have already used an ActionMapping in each one of our prior examples. It is defined by the <action> element in the struts-config.xml file. A sample <action> element is shown in this code snippet:
<action-mappings> <action path="/Lookup" type="ch03.LookupAction" name="lookupForm" validate="true" input="/index.jsp"> <forward name="success" path="/quote.jsp"/> <forward name="failure" path="/index.jsp"/> </action> </action-mappings>
As you may have noticed, we have been using this element throughout this text. The <action> element is how all Action objects are deployed. You should also notice that the <action> element is surrounded by an <action-mappings> element. All <action> elements must be nested within the <action-mappings> element.
The default attributes described by an ActionMapping are defined in Table 8.1.
Attribute | Description |
---|---|
attribute | Represents the name under which the Action's ActionForm bean is bound, if it is other than the bean's specified name attribute. The attribute is often used when there is a need to use the same ActionForm bean instance at the same time. A better way to accomplish this is to create two unique <form-bean> definitions that reference the same ActionForm object. |
className | Represents the fully qualified class name of the ActionMapping implementation class you want to use when invoking this Action class. If the class-Name attribute is not included, then the ActionMapping defined in the ActionServlet's mapping initialization parameter is used. |
exceptions | Specifies a collection of exception handlers that can be associated with a particular ActionMapping. We see this attribute used in Chapter 10, "Managing Errors." |
forward | Represents the context-relative path of the servlet or JSP resource that will process this request using a forward. This attribute is used if you do not want an Action to service the request to this path. The forward attribute is valid only if no include or type attribute is specified. |
type | Represents the fully qualified class name of the Action class being described by this ActionMapping. The type attribute is valid only if no include or forward attribute is specified. |
include | Represents the context-relative path of the servlet or JSP resource that will process this request using an include. This attribute is used if you do not want an Action to service the request to this path. The include attribute is valid only if no forward or type attribute is specified. |
input | Represents the context-relative path of the input form to which control should be returned if ActionErrors are returned from the ActionForm or Action objects. |
name | Identifies the name of the ActionForm bean, if any, that is coupled with the Action being defined. This is not the classname of the ActionForm; it is the unique identifier used to look up the ActionForm object defined by a <form-bean> element. |
path | Represents the context-relative path of the submitted request. The path must start with a / character. |
parameter | Represents a generic configuration parameter that can be used to pass additional information to an Action instance. |
roles | Identifies the collection of user roles that can successfully request this ActionMapping. |
scope | Names the scope of the form bean that is bound to the described Action. |
unknown | If set to true, this ActionMapping instance acts as the default <action-mapping> for the hosting application. |
validate | If set to true, causes the ActionForm.validate() method to be called on the form bean associated to the Action being described. If the validate attribute is set to false, then the ActionForm.validate() method is not called. |
| < Day Day Up > |
|