Creating an action that extends the BaseAction class
In this lesson, you create a Java package for
a class, and a new MyNewAction action class. Then, you add the referrer-cookie functionality
for the new Struts action.
About this task
- 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 uses verbatim). - The flow of MyNewAction's preProcess method is as follows:
- Check whether the referrer cookie exists. If the cookie exists the customer's visit to your store is not the customer's first visit to your store during the predetermined time. Call the BaseAction preProcess method to carry out the WebSphere Commerce specific preprocessing logic and to exit.
- If the referrer cookie does not exist, check who the current referrer is by examining the Referer header of the HTTP request.
- If the referring website is one of the predefined set of referring websites (partner sites), create a referrer cookie with the referring website URL as the value.
- 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 that are 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, which is implemented by the
isPartnerSite
helper method, is limited to pages that contain the string localhost in the page URL. - The referrer cookie that you create uses the default negative
value for its maximum age. The negative value means that the cookie expires
as soon as the browser instance closes. Normally, you would set the
cookie to be stored on the client for a longer length of time, such
as two weeks. A higher value for the maximum age, allows for customers
to browse upon their first visit to your store, and then return to
complete a purchase. To set the maximum age, add the following code
after you create
myCookie
, but before you addmyCookie
to the response:myCookie.setMaxAge( age);
Where age is the time to expiry in seconds.
- The set of partner sites, which is implemented by the
Procedure
- In the Enterprise Explorer view, navigate to WebSphereCommerceServerExtensionsLogic/src.
- Right-click the src folder and select .
- In the New Java Package dialog that opens, enter com.ibm.commerce.sample.struts in the Name field.
- Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field, and click Finish.
- Create the MyNewAction class:
- Navigate to WebSphereCommerceServerExtensionsLogic/src/com.ibm.commerce.sample.struts.
- Right-click the com.ibm.commerce.sample.struts package and select .
- In the New Java Class window, enter MyNewAction in the Name field.
- Click the Browse button next to the Superclass field. In the Superclass Selection window, select the BaseAction class from the com.ibm.commerce.struts package. If you do not see the BaseAction class in the dialog, in the Choose a type file, type BaseAction. In the Matching items dialog, select the BaseAction class from the com.ibm.commerce.struts package.
- Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field and that com.ibm.commerce.sample.struts is specified in the Package field.
- Leave all other options unchanged; click Finish.
- Add the referrer-cookie functionality to the struts action: