Versions Compared

Key

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

...

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

Message Builder

The MessageContext uses a (default) MessageBuilder to create Message s via a fluent API.
If you don't have access to the MessageContext it's possible to use a stand-alone MessageBuilder (SimpleMessageBuilder ).

Note
titleAttention!

Keep in mind that a stand-alone MessageBuilder is able to create Message objects, but it needs a MessageContext to create the final text or to add the message to the context.

Without direct access to the MessageContext you can use a MessageBuilder to create a message via:
Message message = SimpleMessageBuilder.message().text("msgKey").create();
As soon as you have a MessageContext you have access to the final message-text via:
message.toString(messageContext));
And/or you can add it to the context via:
messageContext.addMessage(message);

Custom Message Types

A custom MessageBuilder also allows to introduce custom message types easily:

Code Block
titleCreating custom message types via a custom message-builder
borderStylesolid

Message label = CustomMessageBuilder.label().text("myMsgKey").create();
Message technicalMessage = CustomMessageBuilder.technicalMessage().text("myMsgKey").create();

//the context needs a message-resolver which knows how to handle the different message types.
label.toString(messageContext));
technicalMessage.toString(messageContext));
Code Block
titleImplementation of a custom message-builder
borderStylesolid

public class CustomMessageBuilder extends SimpleMessageBuilder
{
    private CustomMessageBuilder ()
    {
    }

    public static MessageBuilder message()
    {
        return new CustomMessageBuilder();
    }

    public static MessageBuilder technicalMessage()
    {
        return new CustomMessageBuilder().payload(TechnicalMessage.class);
    }

    public static MessageBuilder label()
    {
        return new CustomMessageBuilder().payload(Label.class);
    }
}

A custom MessageResolver has to know how to handle the different message types.

Code Block
titleHandling different message types
borderStylesolid

public class TestMessageResolver implements MessageResolver
{
    public String getMessage(String key, Locale locale, Map<Class, Class<? extends MessagePayload>> messagePayload)
    {
        if (!isKey(key))
        {
            return key;
        }

        try
        {
            key = extractKey(key);

            if(messagePayload.containsKey(Label.class))
            {
                return ResourceBundle.getBundle(TEST_LABELS, locale, getClassLoader()).getString(key);
            }
            else if(messagePayload.containsKey(TechnicalMessage.class))
            {
                return ResourceBundle.getBundle(TEST_TECHNICAL_MESSAGES, locale, getClassLoader()).getString(key);
            }
            return ResourceBundle.getBundle(TEST_MESSAGES, locale, getClassLoader()).getString(key);
        }
        catch (MissingResourceException e)
        {
            return key;
        }
    }
    //The full example is available in the code-base (see: TestMessageResolver)
}

Manual Usage

If you don't like the fluent API you can create your own message (e.g. based on DefaultMessage) or you can just instantiate DefaultMessage.

Code Block
titleCreating a message manually
borderStylesolid

Message message = new DefaultMessage("inline message");