Creating the error message text

In this lesson, you define the new error message and the associated properties files for the message.

Procedure

  1. Create the properties file that contains the message information:
    The properties file contains the text that displays in the web browser. Properties files facilitate translation, since the text is separated from the code, the file can be translated without affecting the Java code.
    1. Open IBM WebSphere Commerce Developer.
    2. In the Enterprise Explorer view, expand Stores > Java Resources > src.
    3. Right click the src folder, select New > Other.
    4. Select General > File and click Next.
    5. In the File name field, enter MyNewErrorMessages.properties, then click Finish.
    6. Copy the following text into the new file:
      
      _ERR_TOO_MANY_ITEMS=You cannot add more items into your shopping cart. Your shopping cart can hold up to five different items. 
      
    7. Save your changes.
  2. Create the Java code that references the properties file.
    1. Expand WebSphereCommerceServerExtensionsLogic > src.
    2. Right click the src folder, select New > Package. Enter the following information:
      1. Enter com.ibm.commerce.sample.messages In the Name field.
      2. Click Finish.
    3. In Enterprise Explorer view, right click the com.ibm.commerce.sample.messages package, select New > Class.
    4. In the New Java Class wizard:
      1. Enter MyNewMessages in the Name field.
      2. Click Finish. The MyNewMessages class opens for editing.
    5. Add the following code within the default class implementation:
      
      // Resource bundle used to extract the text for an exception
        static final String errorBundle = "MyNewErrorMessages";
      
        // An ECMessage describes an ECException and is passed
        // into the ECException when thrown
        public static final ECMessage _ERR_TOO_MANY_ITEMS =
            new ECMessage(ECMessageSeverity.ERROR, ECMessageType.USER, 
            MyNewMessageKeys._ERR_TOO_MANY_ITEMS, errorBundle);
      
    6. From the Source menu, select Organize Imports to add the following import statements to the class:
      
      import com.ibm.commerce.ras.ECMessage;
      import com.ibm.commerce.ras.ECMessageSeverity;
      import com.ibm.commerce.ras.ECMessageType;
      
      Your completed class can resemble the following code:
      
      package com.ibm.commerce.sample.messages;
      
      import com.ibm.commerce.ras.ECMessage;
      import com.ibm.commerce.ras.ECMessageSeverity;
      import com.ibm.commerce.ras.ECMessageType;
      
      public class MyNewMessages {
      
      // Resource bundle used to extract the text for an exception
      static final String errorBundle = "MyNewErrorMessages";
      
      // An ECMessage describes an ECException and is passed
      // into the ECException when thrown
      public static final ECMessage _ERR_TOO_MANY_ITEMS =
      new ECMessage(ECMessageSeverity.ERROR, ECMessageType.USER, 
      MyNewMessageKeys._ERR_TOO_MANY_ITEMS, errorBundle);
      }
      
    7. Save your changes.
    8. Right click the com.ibm.commerce.sample.messages package, select New > Class.
    9. In the New Java Class wizard:
      1. Enter MyNewMessageKeys in the Name field.
      2. Click Finish. The MyNewMessageKeys class opens for editing.
    10. Add the following code within the default class implementation:
      public class MyNewMessageKeys {
      // This class defines the keys used to create new exceptions that are 
      // thrown by customized code.
        public static final String _ERR_TOO_MANY_ITEMS = "_ERR_TOO_MANY_ITEMS";
      }
      
      Your completed classs can resemble the following code:
      
      package com.ibm.commerce.sample.messages;
      
      public class MyNewMessageKeys {
      // This class defines the keys used to create new exceptions that are 
      // thrown by customized code.
      public static final String _ERR_TOO_MANY_ITEMS = "_ERR_TOO_MANY_ITEMS";
      }
    11. Save your changes.