The Action Class

 < Day Day Up > 



The most common component of a Struts Controller is the org.apache.struts.action.Action class. As we stated in Chapter 3, the Action class must and will be extended for each specialized Struts function in your application. The collection of these Action classes is what defines your Web application.

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.

Extending the Action Class

Now that you have seen the steps required to create an Action class, let's take a look at how you can create your own Action class. Here's a skeleton Action class implementation:

 package ch04; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class SkeletonAction extends Action {   // Add your Action class implementation } 

As you can see, there is really nothing special about this step--you simply create a Java object that extends the org.apache.struts.action.Action class. In the remainder of this book, we extend the Action class.

Implementing the execute() Method

The execute() method is where your application logic begins. It is the method that you need to override when defining your own Struts Actions. The execute() method has two functions:

  • It performs the user-defined business logic associated with your application.

  • It tells the Framework where it should next route the request.

The Struts Framework defines two execute() methods. The first execute() implementation is used when you are defining custom Actions that are not HTTP specific. This implementation of the execute() method is analogous to the javax.serlvet.GenericServlet class; its signature is:

 public ActionForward execute(ActionMapping mapping,                                  ActionForm form,                                  ServletRequest request,                                  ServletResponse response)         throws IOException, ServletException 

Notice that this method receives, as its third and fourth parameter, a ServletRequest and a ServletResponse object, as opposed to the HTTP-specific equivalents HttpServletRequest and HttpServletResponse. This is the version of the Action.execute() method that you need to override when servicing requests that are not HTTP specific.

start sidebar

If you do receive a non-HTTP request to a Action that does not implement the previous execute() method, then the Framework will attempt to coerce the ServletRequest and ServletResponse objects into HttpServletRequest and HttpServletResponse objects, respectively, and then call the HTTP-specific perform() method. If you would like to see the source that performs this task, download the source, open the Action.java source file, and look at the execute() methods within it.

end sidebar

The second execute() implementation is used when you are defining HTTP-specific custom Actions. This implementation of the method is analogous to the javax.servlet.http.HttpServlet class; its signature is:

 public ActionForward execute(ActionMapping mapping,                                  ActionForm form,                                  HttpServletRequest request,                                  HttpServletResponse response)         throws IOException, ServletException 

Notice that this method (unlike the first one) receives, as its third and fourth parameter, an HttpServletRequest and an HttpServletResponse object. This implementation of the execute() method is the one that you will most often extend. Table 4.2 describes all of the parameters of the Action.execute() method.

Table 4.2: The Parameters of the Action.execute() Method

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.

After examining the parameters passed to the execute() method, you should take a look at its return type, the ActionForward object. This object is used by the RequestProcessor to determine where the request will go next, whether it is a JSP or another Action. The following code snippet shows an example execute() method:

 package ch04; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class SkeletonAction extends Action {   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws IOException, ServletException {     if ( form != null ) {       // 1. Get a reference to the ActionForm       SkeletonForm actionForm = (SkeletonForm)form;       // 2. Add Your Business Logic     }     // 3. return the appropriate ActionForward     return (mapping.findForward("success"));   } } 

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. You see how ActionForms are associated with Actions in the next section of this chapter.

  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

Now that you have seen how an Action class is created, let's examine its configuration options. The Action class is a Struts-specific object and therefore must be configured using the struts-config.xml file.

The element that is used to describe a Struts action is an <action> element. The class that defines the <action> element's attributes is the org.apache.struts.action.ActionMappings class. In Chapter 8, "Working with Custom ActionMappings," you see how this class can be extended to define additional <action> attributes. Table 4.3 describes the attributes of an <action> element as they are defined by the default ActionMappings class.

Table 4.3: Attributes of an <action> Element

Attribute

Description

attribute

Names a request or session scope attribute that is used to access an Action-Form bean, if it is other than the bean's specified "name".

className

Names the fully qualified class name of the ActionMapping implementation class you want to use in when invoking this Action class. If the className attribute is not included, the ActionMapping defined in the ActionServlet's mapping initialization parameter is used. We use the attribute when we create a new ActionMappings implementation in Chapter 8.

forward

Represents a Module-relative path of the servlet or JSP resource that will process this request. 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.

include

Represents a Module-relative path of the servlet or JSP resource that will process this request. 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 a Module-relative path of the input form to which control should be returned if a validation error is encountered. The input attribute is where control will be returned if ActionErrors are returned from the ActionForm or Action objects. (Optional)

name

Identifies the name of the form bean that is coupled with the Action being defined.

path

Represents the Module-relative path of the submitted request. The path must be unique and start with a / character.

parameter

A generic configuration parameter that is used to pass extra information to the Action object defined by this action mapping.

roles

A comma-delimited list of security roles that can access the defined <action /> object.

type

Names 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.

scope

Names the scope of the form bean that is bound to the described Action. The possible values are request or session. The default value is session.

unknown

If set to true, this <action /> is used as the default action mapping when a request does not match another <action /> element. Only one ActionMapping can be marked as unknown per Module.

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. The default value is true.

start sidebar

When using an <action> element to describe an Action class, you are describing only one instance of the named Action class. There is nothing stopping you from using n-number of <action> elements that describe the same Action class. The only restriction is that the path attribute must be unique for each <action> element.

end sidebar

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> 

start sidebar

All <action> elements must be defined as sub-elements of <action-mappings />.

end sidebar

This <action> element tells the ActionServlet the following things about this Action instance:

  • The Action class is implemented by the ch04.LookupAction class.

  • This Action should be invoked when the URL ends with the path /Lookup.

  • This Action class will use the <form-bean> with the name lookupForm.

  • The originating resource that submitted the request to this Action is the JSP index.jsp.

  • This Action class will forward the results of its processing to either quote.jsp or index.jsp depending on the returned ActionForward.

The previous <action> element uses only a subset of the possible <action> element attributes, but the attributes that it does use are some of the most common ones.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net