• No results found

The Action Class

In document J2EE (Page 147-151)

The second component of a Struts Controller is the org.apache.struts.action. Action class must and will be extended for each specialized Struts function in our application. The collection of the Action classes that belong to our Struts application is what defines our Web application.

To begin our discussion of the Action class, we must first look at some of the Action methods that are more commonly overridden or leveraged when creating an extended Action class. The following sections describe five of these methods.

The execute() Method

The execute() method is where our application logic begins. It is the method that we need to override when defining our own Actions. The Struts framework defines two execute() methods.

The first execute() implementation is used when we are defining custom Actions that are not HTTP-specific. This implementation of the execute() method would be analogous to the javax.serlvet.GenericServlet class. The signature of this execute() method is

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

throws IOException, ServletException

We will 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.

The second execute() implementation is used when we are defining HTTP- specific custom Actions. This implementation of the execute() method would be analogous to the javax.serlvet.http.HttpServlet class. The signature of this execute() method is

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

We will notice that this method receives, as its third and fourth parameter, a HttpServletRequest and a HttpServletResponse object, as opposed to the previously listed execute() method. This implementation of the execute() method is the implementation that we will most often extend.

Parameters of the Action.execute() method.

ActionMapping : Contains all of the deployment information for a particular Action bean. This class will be used to determine where the results of the LoginAction will be sent after 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 : Is a reference to the current HTTP request object.

HttpServletResponse : Is a reference to the current HTTP response object.

Extending the Action Class

Now that we have seen the Action class and some of its configuration options, let’s see how we can create our own Action class.

To develop our own Action class, we must complete the following steps. These steps describe the minimum actions that must be completed when creating a new Action:

1. Create a class that extends the org.apache.struts.action.Action class.

2. Implement the appropriate execute() method and our specific to our business logic.

3. Compile the new Action and move it into the Web application’s classpath.

4. Add an <action> element to the application’s struts-config.xml file describing the new Action.

An example execute() implementation is listed in the following snippet. We will be extending the Action class throughout the remainder of this text.

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

Double price = null;

// Default target to success

String target = new String("success");

if ( form != null ) {

// Use the LookupForm to get the request parameters LookupForm lookupForm = (LookupForm)form;

String symbol = lookupForm.getSymbol();

price = getQuote(symbol);

}

// Set the target to failure if ( price == null ) {

// Forward to the appropriate View return (mapping.findForward(target));

}

Configuring the Action Class

Now that we have seen the major methods of the Action class, 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 configure a Struts action is an <action>

element. The class that defines the <action> element’s attributes is the org.apache.

struts.action.ActionMappings class. Next Table describes the attributes of an <action> element as they are defined by the default ActionMappings class.

Note When using an <action> element to describe an Action class, we are describing only one instance of the named Action class. There is nothing stopping we 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.

Path : Represents the context-relative path of the submitted request. The path must be unique and start with a / character. (required)

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. (optional)

name : Identifies the name of the form bean, if any, that is coupled with the Action being defined. (optional)

scope : Names the scope of the form bean that is bound to the described Action.

The default value is session. (optional)

input : Names the context-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)

className : Names the fully qualified class name of the ActionMapping

implementation class 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. (optional) forward : Represents the context-relative path of the servlet or JSP resource that will

process this request. This attribute is used if we 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. (optional)

include : Represents the context-relative path of the servlet or JSP resource that will process this request. This attribute is used if we 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. (optional)

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. (optional)

A sample <action> subelement using some of the previous attributes is shown here:

<action-mappings>

<action path="/Lookup"

type="wiley.LookupAction"

name="lookupForm"

input="/index.jsp">

<forward name="success" path="/quote.jsp"/>

<forward name="failure" path="/index.jsp"/>

</action>

</action-mappings>

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

 The Action class is implemented by the wiley.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 the quote.jsp or the index.jsp.

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 more common.

In document J2EE (Page 147-151)