!--a11y-->
Example for Using Messages 
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.
...
For a detailed description of how to create UI elements in the SAP NetWeaver Developer Studio, see Working with User Interface Elements.
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 property cardinality 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.

For a detailed description of how to create context structures in the SAP NetWeaver Developer Studio, see Context Structure.
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 |
|
b |
Input Field |
B |
|
Children_2 |
Input Field |
TypeField |
|
In addition, bind the Children_3 pushbutton to action ValidateAction, which you also have to create. For information on creating actions, see Creating an Action at Design Time.
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}!
|
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:
Implementation of Method 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 } |
The following tasks have been
implemented in this method: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, deployed, and run your project, you start the Web application.

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:

