Tutorials > Program model > Extend a Struts action

< Previous | Next >


Create an action extending BaseAction

This section is about creating a Java package for your class, creating the MyNewAction class and adding the referrer-cookie functionality.


Procedure

  1. In the Enterprise Explorer view, navigate to Other Projects > WebSphereCommerceServerExtensionsLogic > src.

  2. Right-click the src folder and select New > Package.

  3. In the New Java Package dialog that opens, type com.ibm.commerce.sample.struts in the Name field.

  4. Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field, and click Finish.

  5. Create the MyNewAction class

    1. Back in the Enterprise Explorer view, navigate to Other Projects > WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.struts.

    2. Right-click the com.ibm.commerce.sample.struts package and select New > Class.

    3. In the New Java Class dialog that opens, do the following:

      1. Type MyNewAction in the Name field.

      2. Click the Browse button next to the Superclass field. In the Superclass Selection dialog that opens, select the BaseAction class from the com.ibm.commerce.struts package.

      3. Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field and com.ibm.commerce.sample.struts is specified in the Package field.

      4. Leave the other options unchanged and click Finish.

  6. Add the referrer-cookie functionality

    1. If it does not automatically open for editing, double-click MyNewAction.java in the Enterprise Explorer view under Other Projects > WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.struts.

    2. Copy the following code snippet into the body of the class:

      protected TypedProperty preProcess(RequestHandle handle,                         
          ActionMapping mapping, ActionForm form, Map
          requestParameters,                         
          HttpServletRequest request, 
          HttpServletResponse response,                         
          String viewName) throws RequestException {
      
                      /// See if the referrer cookie already exists.
      
                      // Get all the cookies the client has sent with this request.
                      Cookie[] cookies = request.getCookies();
      
                      // If there are any, see if the referrer cookie is one of them.
                      if (cookies != null) {
                              for (int i = 0; i < cookies.length; i++)
                      {
                          String name = cookies[i].getName();
      
                          // If it is, done; carry on with BaseAction's preprocessing.
                          if (name.equals(ECConstants.EC_COOKIE_ExternalReferrer)) {
                             return super.preProcess(handle, mapping, form, requestParameters, request, response, viewName);
                                      }
                              }
                      }
      
                      /// No cookies at all or no referrer cookie.
      
                      // Get referrer info from header.
                      String referrerURL = request.getHeader(ECConstants.EC_HTTP_ReferrerHeader);
      
                      // If the referrer is a partner site ...
                      if (isPartnerSite(referrerURL)) {
      
                              // ... create a new cookie
                              Cookie myCookie = new Cookie( ECConstants.EC_COOKIE_ExternalReferrer, referrerURL);
      
                              // ... add it to the response.
                              response.addCookie(myCookie);
                      }
      
                      // Carry on with BaseAction's preprocessing.
                      return super.preProcess(handle, mapping, form, requestParameters, request, response, viewName);
      
              }
      
          private boolean isPartnerSite(String url) {
          return ((url != null) && (url.indexOf("localhost") != -1));
              }
      

    3. Examine the source code and note the following:

      • MyNewAction class extends BaseAction by adding functionality to the preProcess method.

      • The preProcess method of MyNewAction has a signature identical to that of BaseAction, as expected by the BaseAction's execute method (which MyNewAction will use verbatim).

      • The flow of MyNewAction's preProcess method is...

        1. Check whether the referrer cookie already exists. If it does, this is not the customer's first visit to the page in the predetermined period of time; call BaseAction's preProcess method to carry out the WebSphere Commerce - specific pre-processing logic, and exit.

        2. If the referrer cookie does not exist, check who the current referrer is by examining the Referer header of the HTTP request.

        3. If the referring Web site is one of the predefined set of referring Web sites (partner sites), create a new referrer cookie with the referring Web site's URL as its value.

        4. Call BaseAction's preProcess method to carry out the WebSphere Commerce - specific pre-processing logic, and exit.

      • The preceding implementation uses the EC_COOKIE_ExternalReferrer and EC_HTTP_ReferrerHeader constants defined in the WebSphere Commerce ECConstants class for the name of the referrer cookie and the name of the referrer header, respectively.

      • To make testing easier, the preceding snippet makes the following simplifications:

        • The set of partner sites, implemented by the isPartnerSite helper method, is limited to pages with the string localhost in their URL.

        • The referrer cookie created uses the default negative value for its maximum age, which means the cookie will expire as soon as the browser instance is closed. In a realistic scenario, you would want the cookie to be stored on the client for a period of time on which you have agreed with the partner sites (for instance, two weeks), to allow for the fact that customers might browse only during their initial visit and make a purchase during a subsequent visit.

          To do so, add the following statement after creating myCookie and before adding it to the response:

          myCookie.setMaxAge(
          age);
          

          where age is the time to expiry in seconds.

    4. Back in the MyNewAction.java editor window, notice several errors reported by WebSphere Commerce Developer.

    5. Resolve these errors by adding appropriate import statements as follows:

      1. From the Source menu, select Organize Imports.

      2. In the Organize Imports pages that open, select java.util.map, com.ibm.commerce.datatype.TypedProperty, javax.servlet.http.Cookie, and com.ibm.commerce.server.ECConstants.

    6. Save the changes.

< Previous | Next >


+

Search Tips   |   Advanced Search