The following example shows how you can use messages created in the Message Editor. In the example, both messages with static text and messages that are dependent on user inputs – that is, messages with dynamic text – are defined.
Users can create a domain in this sample application. They can then enter a number in the next input field and press Click here to validate. If the specified number lies in the previously specified range, the user is informed of this fact in a standard message. If the number does not lie within this domain, the user sees a warning message.
You have created a Web Dynpro application and defined view “MainView” within a Web Dynpro component.
...
Define the view as illustrated below:
The context that provides the data is created as follows:
...
1. Create a context node, UIElem
2. Set the propertycardinality to 1..1 for the context node.
3. Create the context attributes a, b, and TypeField.
4. Set the Type of the context attributes to Integer.
To make the messages dynamic with regard to specification of the domain, the user inputs have to be saved. To do this, the input fields have to be bound to the context.
sss
Object |
Object ID |
Data Binding to Attribute |
Path Within the Context Structure |
a |
Input Field |
A |
MainView.UIElem.a |
b |
Input Field |
B |
MainView.UIElem.b |
Children_2 |
Input Field |
TypeField |
MainView.UIElem.TypField |
In addition, bind the Children_3 pushbutton to action ValidateAction, which you also have to create.
The Web Dynpro tools provide a special message editor for defining messages of different types.
A message is defined by a specified key, message type, and message text. The message types error, warning, and standard are predefined.
Create the following messages:
Messages Defined in the Message Editor |
||
Message Key |
Message Type |
Message text |
key1 |
warning |
Please enter a number between the range of {0} and {1}! {0} and {1} are placeholders for the user input (the domain), which changes dynamically. |
key2 |
standard |
The value entered is within the valid range. |
Because the messages are only displayed when the user Chooses Click here to validate, the messages must be implemented in the method onActionValidateAction:
Implementierung der Methode onActionValidateAction() |
//@@begin imports import com.sap.tc.webdynpro.progmodel.controller.MessageManager; import com.sap.test.errorhandlingtest1.wdp.IMessageErrorhandlingTest1; import com.sap.test.errorhandlingtest1.wdp.IPrivateMainView; //@@end //...
public void onActionValidateAction( com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent) { //@@begin int i = wdContext.currentUIElemElement().getTypField(); int a = wdContext.currentUIElemElement().getA(); int b = wdContext.currentUIElemElement().getB();
MessageManager msgMgr = (MessageManager)wdThis.wdGetAPI().getComponent().getMessageManager();
if (a < i && i < b) { msgMgr.reportMessage(IMessageErrorhandlingTest1.KEY2, null, true); } else { Object[] arg ={new Integer(a), new Integer(b)}; msgMgr.reportMessage(IMessageErrorhandlingTest1.KEY1, arg, true); } //@@end } |
1. Read user inputs:
int I = wdContext.currentUIElemElement().getTypField(); int a = wdContext.currentUIElemElement().getA(); int b = wdContext.currentUIElemElement().getB(); |
1. Read messages from the Message Manager:
MessageManager msgMgr = (MessageManager) wdThis.wdGetAPI().getComponent().getMessageManager();
|
2. Does the input lie within the defined domain?
if (a < i && I < b) |
3. Call the standard message when the input lies within the domain:
MsgMgr.reportMessage(IMessageErrorhandlingTest1.KEY2, null); |
Method reportMessage can be used to read the messages from the Message Manager. In this way you define the key and the objects that you want to change dynamically in the messages. Because no dynamic text was defined in your standard messages, you define null as a parameter.
4. Call the warning messages when the input does not lie within the domain:
Object[] arg ={new Integer(a), new Integer(b)}; MsgMgr.reportMessage(IMessageErrorhandlingTest1.KEY1, arg); |
Because the warning messages ( Please enter a number between the range of {0} and {1}!) contain text that depends on the user input, you also have to define the parameters for the domain in an object array. In the messages, the first object is then called from the array with {0} (and the second with {1}, and so on).
After you have built and deployed your application, you can call it by choosing Run.
If the user enters a number that lies within the defined domain, a standard message is displayed:
If the user enters a number that does not lie within the defined domain, a warning message is displayed: