Create message information
Your new command implementation uses a new error
message:
_ERR_TOO_MANY_ITEMS
. In this step, you create
the code for a new error message and its associated properties file.About this task
Procedure
- 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.
- In the Enterprise Explorer view, navigate to Stores > Java Resources > src.
- Right click the src folder, select New > Other.
- Select General > File and click Next.
- In the File name field, enter
MyNewErrorMessages.properties
, then click Finish. - 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.
- Save your changes.
- Create the Java code that references the properties file.
- In WebSphere Commerce Developer, in the Enterprise Explorer view, navigate to Utility Projects > WebSphereCommerceServerExtensionsLogic > src.
- Right click the src folder, select New > Package.
Enter the following information:
- In the Name field, enter
com.ibm.commerce.sample.messages
. - Click Finish.
- In the Name field, enter
- In Enterprise Explorer view, right click the com.ibm.commerce.sample.messages package, select New > Class.
- In the New Java Class wizard:
- In the Name field, enter
MyNewMessages
. - Click Finish. The MyNewMessages class opens for editing.
- In the Name field, enter
- Within the class, add the following code:
// 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);
- From the Source menu, select Organize
Imports to add the following required import statements
to the class:
import com.ibm.commerce.ras.ECMessage; import com.ibm.commerce.ras.ECMessageSeverity; import com.ibm.commerce.ras.ECMessageType;
- Save your changes.
- Right click the com.ibm.commerce.sample.messages package, select New > Class.
- In the New Java Class wizard:
- In the Name field, enter
MyNewMessageKeys
. - Click Finish.
- In the Name field, enter
- Replace the following default class implementation:
with the following code:public class MyNewMessageKeys { }
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"; }
- Save your changes.