To develop your own Action class, you must complete the following steps. These steps describe the minimum actions that you must take when creating a new Action and are the focus of this section:
1.Create a class that extends the org.apache.struts.action.Action class.
2.Implement the appropriate execute() method and add your specific business logic.
3.Compile the new Action and move it into the Web application's classpath. This would most often be your WEB-INF/classes directory.
4.Add an <action> element to the application's struts-config.xml file describing the new Action.
Implementing the execute() Method :
The execute() method has two functions:
Component | Description |
---|---|
ActionMapping | Contains all of the deployment information for a particular Action bean. This class is to determine where the results of the LoginAction will be sent once its processing is complete. |
ActionForm | Represents the Form inputs containing the request parameters from the View referencing this Action bean. The reference being passed to our LoginAction points to an instance of our LoginForm. |
HttpServletRequest | A reference to the current HTTP request object. |
HttpServletResponse | A reference to the current HTTP response object. |
As you build your own Action objects, you will notice that they almost all perform the same sequence of events.
1.Cast the ActionForm reference associated with this Action to your specific ActionForm implementation. In the previous example, we are casting the passed-in ActionForm object to a SkeletonForm.
2.Add your specific business logic.
3.Use the ActionMapping.findForward() method to find the ActionForward object that matches the <forward> sub-element in the <action> definition. We look at the <forward> sub-element in the following section.
4.Return the retrieved ActionForward object. This object routes the request to the appropriate View.
Configuring the Action Class :
A sample <action> sub-element using some of the previous attributes is shown here:
<action-mappings> <action path="/Lookup" type="ch04.LookupAction" name="lookupForm" input="/index.jsp"> <forward name="success" path="/quote.jsp"/> <forward name="failure" path="/index.jsp"/> </action> </action-mappings>
-
It performs the user-defined business logic associated with your application.
-
It tells the Framework where it should next route the request.
The Parameters of the Action.execute() Method :