In this lesson, you define the new error message and the associated properties files for
the message.
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.
-
Open IBM WebSphere Commerce Developer.
-
In the Enterprise Explorer view, expand .
-
Right click the src folder, select .
-
Select 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.
-
Expand .
-
Right click the src folder, select . Enter the following information:
- Enter
com.ibm.commerce.sample.messages
In the Name
field.
- Click Finish.
-
In Enterprise Explorer view, right click the
com.ibm.commerce.sample.messages package, select New >
Class.
-
In the New Java Class wizard:
- Enter
MyNewMessages
in the Name field.
- Click Finish. The MyNewMessages class opens for
editing.
-
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);
-
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);
}
-
Save your changes.
-
Right click the com.ibm.commerce.sample.messages package, select
New > Class.
-
In the New Java Class wizard:
- Enter MyNewMessageKeys in the Name field.
- Click Finish. The MyNewMessageKeys class opens for
editing.
-
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";
}
-
Save your changes.