Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

MessageContext

The MessageContext allows using a fluent API for

...

Furthermore, the context provides the current locale.

Message

The default implementation provides

...

The Message interface extends Localizable . Therefore it's possible to use a MessageContext to create the final message text based on the message and the current configuration of the MessageContext .

Creating messages

Instead of instantiating messages manually, the fluent API allows to create messages with (named-)arguments and message payload.

Example 1

Code Block
titleCreating messages with inline-text
borderStylesolid
Message message = messageContext.message().text("Hello MyFaces").create();

... creates a message with the hardcoded text 'Hello MyFaces' as message descriptor.

Example 2

Code Block
titleCreating messages with a key
borderStylesolid
Message message = messageContext.message().text("msgKey").create();

... creates a message with the key 'msgKey' as message descriptor.
(The concrete syntax of a key depends on the used MessageResolver. Further details are available in the DevDoc.)

Example 3

Code Block
titleCreating messages with arguments
borderStylesolid
Message message = messageContext.message().text("Hello {0}").argument("MyFaces").create();

... creates a message with 'Hello {0}' as message descriptor and 'MyFaces' as argument.
An argument has to be Serializable .
(By default numbered arguments are formatted by MessageFormat based on the current locale.)

Example 4

Code Block
titleCreating messages with named arguments
borderStylesolid
Message message = messageContext.message().text("Hello {name}").namedArgument("name", "MyFaces").create();

... creates a message with 'Hello {name}' as message descriptor and 'MyFaces' as argument.
Values of named arguments are formatted by Formatter s created by the FormatterFactory

Example 5

Code Block
titleCreating messages with payload
borderStylesolid
Message message = messageContext.message().text("Hello CODI").payload(MessageSeverity.Error.class).create();

... creates a message with 'Hello CODI' as message descriptor and error as message-severity.
Payload is based on the validation payload of Bean-Validation. Via payload it's possible to provide additional information via a typesafe mechanism. It's possible to use the provided types of payload (MessageSeverity or InternalMessage) or to create custom payload like Label . It's possible to handle messages depending on the payload of the message. By default a message is created with MessageSeverity.Info .

Creating message text

Example 1

Code Block
titleCreating the message-text (immediately)
borderStylesolid
String messageText = messageContext.message().text("msgKey").toText();

... creates the message-text based on the constructed message.

Example 2

Code Block
titleCreating the message-text of a given message
borderStylesolid
//Message message = messageContext.message().text("Hello {name}").namedArgument("name", "MyFaces").create();
String messageText = message.toString(messageContext);

... creates the message-text based on the given message and MessageContext .

Processing messages

It's possible to register MessageHandler s which process or forward messages added to the MessageContext .

E.g. a MessageHandler for JSF would convert messages to FacesMessage s and add them to the current FacesContext .

Example 1

Code Block
titleCreating the message-text (immediately)
borderStylesolid
messageContext.message().text("Hello MyFaces").add();

... adds the constructed message to the MessageHandler (s) of the current MessageContext .

Example 2

Code Block
titleCreating the message-text of a given message
borderStylesolid
//Message message = messageContext.message().text("Hello {name}").namedArgument("name", "MyFaces").create();
messageContext.addMessage(message);

... adds the given message to the MessageHandler (s) of the current MessageContext .

Message Builder

Manual Usage