Creating a Custom ActionMapping

 < Day Day Up > 



In the previous section, we saw that the default ActionMapping defines quite a number of attributes. The combination of each of these attributes is used to uniquely describe an Action instance, and in most cases, these default attributes suffice. However, there are times when you need to further describe an Action.

The Struts Framework does provide a solution for this very problem by allowing you to define properties specific to your application needs. You can define these supplementary properties by simply extending the ActionMapping class and adding n-number of <action> subelements and <set-property> elements for each additional property.

start sidebar

The Struts Framework provides two extended ActionMapping classes for developer convenience. These two ActionMapping extensions—org.apache. struts.action.SessionActionMapping and org.apache.struts.action. RequestActionMapping—default the form bean scope to session and request, respectively, relieving the developer from specifically setting the ActionMapping scope attribute.

end sidebar

Creating an ActionMapping Extension for the wroxapp Application

To see how a new ActionMapping is created, let's create our own ActionMapping extension that we can use to describe the Actions of our wroxapp application. The ActionMapping extension that we create will allow us to turn logging on or off in our ch03.LookupAction by using a single <set-property> element.

To create an ActionMapping extension, perform these steps:

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

  2. Define the additional properties that will be used to describe your Action objects.

  3. Call the super() method, which calls the ActionMapping's default constructor, at the beginning of your ActionMapping's constructor.

  4. Define matching setters and getters that can be used to modify and retrieve the values of the defined properties.

The source for our new WroxActionMapping is shown in Listing 8.1. As you look over this class, notice that it is a very simple class that satisfies the previous steps. It defines a single Boolean attribute, logResults, which we use in our ch03.LookupAction to determine whether or not it should log its results.

Listing 8.1: WroxActionMapping.java.

start example
 package ch08; import org.apache.struts.action.ActionMapping; // Step 1. Extend the ActionMapping class public class WroxActionMapping extends ActionMapping {   // Step 2. Add the new properties   protected boolean logResults = false;   public WroxActionMapping() {     // Step 3. Call the ActionMapping's default Constructor     super();   }   // Step 4. Add matching setter/getter methods   public void setLogResults(boolean logResults) {     this.logResults  = logResults;   }   public boolean getLogResults() {     return logResults;   } } 
end example

Using the ch08.WroxActionMapping Extension in the wroxapp Application

To leverage our new ActionMapping, we need to make the appropriate modifications to our LookupAction. The changes that we want to make are both in the source file and the <action> element describing the LookupAction. The first change is to the actual LookupAction source code and is shown in Listing 8.2.

Listing 8.2: The modified ch03.LookupAction.java.

start example
 package ch03; 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; import ch08.WroxActionMapping; public class LookupAction extends Action {   protected Double getQuote(String symbol) {     if ( symbol.equalsIgnoreCase("SUNW") ) {       return new Double(25.00);     }     return null;   }   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws IOException, ServletException {     WroxActionMapping WroxMapping =      (WroxActionMapping)mapping;     Double price = null;     String symbol = null;     // Default target to success     String target = new String("success");     if ( form != null ) {       LookupForm lookupForm = (LookupForm)form;       symbol = lookupForm.getSymbol();       price = getQuote(symbol);     }      // if price is null, set the target to failure     if ( price == null ) {       target = new String("failure");     }     else {       if ( wroxMapping.getLogResults() ) {         System.err.println('"SYMBOL:"           + symbol + " PRICE:" + price);       }       request.setAttribute("PRICE", price);     }     // Forward to the appropriate View     return (mapping.findForward(target));   } } 
end example

We need to examine two sections of code from Listing 8.2. The first section takes the ActionMapping instance passed to the execute() method and casts it to a WroxActionMapping. We can do this because we know that this class is really an instance of the WroxActionMapping and we must do it to get access to the getLogResults() method. The casting that it performs is shown in the following snippet:

 WroxActionMapping wroxMapping =   (WroxActionMapping)mapping; 

The second section of code uses the value retrieved from the getLogResults() method to determine whether it should log the results of its actions. If the value is true, the action logs its results, which in this case is simply a write to the System.err stream; otherwise, it skips over the System.err.println() statement. The following snippet shows this test:

 if ( wroxMapping.getLogResults() ) {   System.err.println("SYMBOL:"     + symbol + " PRICE:" + price); } 

Deploying the ch08.WroxActionMapping Extension

Deploying an ActionMapping extension is also a simple process. The first thing that you need to do is compile the ActionMapping class and place it in the application classpath. For our example, compile the ch08.WroxActionMapping class and move it to the <CATALINA_HOME>/webapps/wroxapp/ WEB-INF/classes/ch08/ directory.

You then need to tell the Controller about the new ActionMapping. You can accomplish this by using two different methods. The first method allows you to define an ActionMapping on the global level using the type attribute of the <action-mappings> element. An example of this method is shown in this code snippet:

 <action-mappings type="ch08.WroxActionMapping"> 

When using this method, all actions in the hosting application will use the ch08.WroxActionMapping.

The second custom ActionMapping deployment method allows you to configure a custom ActionMapping on a more granular level—on an <action> by <action> basic. When using this deployment method, you use the className attribute of the <action> sub-element, as shown here:

 <action className="ch08.WroxActionMapping"> 

This <action> by <action> deployment is the method that we will use in our example application. To see these changes working together, let's make one last modification. This modification is made to the actual <action> element that describes the ch03.LookupAction instance. The change itself includes:

  • The addition of the className attribute to the <action> element

  • The addition of the <set-property> element, with its property attribute set to the matching ch08.WroxActionMapping data member and its value attribute set to the value that you want the property set to.

    The following code snippet shows this modification:

     <action className="ch08.WroxActionMapping">   path="/Lookup"   type="ch03.LookupAction"   name="lookupForm"   input="/index.jsp">   <set-property property="logResults" value="true"/>   <forward name="success" path="/quote.jsp"/>   <forward name="failure" path="/index.jsp"/> </action> 

start sidebar

If you define an ActionMapping extension that includes more than one property, you must add a <set-property> element for each additional property.

end sidebar

The result of this entry is a WroxActionMapping instance with a logResults data member set to true.

Now you simply need to restart Tomcat and go through the normal process of looking up the SUNW stock symbol. If everything went according to plan, you should see the following output in the Tomcat console window:

 SYMBOL:SUNW PRICE:25.0 



 < 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