...
Note |
---|
|
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); |
Advanced Use-Cases
Custom Message Types
A custom MessageBuilder
also allows to introduce custom message types easily:
...
Code Block |
---|
title | Handling different message types |
---|
borderStyle | solid |
---|
|
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)
}
|
Business client specific messages
As soon as an application has to support different business clients, it's required to display messages which are specific to each business client. The message module allows to use one MessageResolver
per client. So it's possible to use the same message-keys for all clients and the concrete MessageResolver
provides the correct message.
In combination with CDI a possible implementation is illustrated in the following listings.
Code Block |
---|
title | Provider of the current business client |
---|
borderStyle | solid |
---|
|
@Produces
@RequestScoped
public Client getCurrentClient(ClientService clientService)
{
return clientService.loadClient(this.currentClientId);
}
|
Code Block |
---|
title | Business client specific messages |
---|
borderStyle | solid |
---|
|
@Produces
@RequestScoped
@ClientQualifier
public MessageContext createClientAwareContext(@Jsf MessageContext messageContext,
Client client)
{
MessageResolver clientAwareMessageResolver = createMessageResolverForClient(client.getId());
return messageContext.config().use().messageResolver(clientAwareMessageResolver).create();
}
private MessageResolver createMessageResolverForClient(final String currentClientId)
{
return new MessageResolver()
{
public String getMessage(String messageDescriptor,
Locale locale,
Map<Class, Class<? extends MessagePayload>> messagePayload)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
try
{
return facesContext.getApplication()
.getResourceBundle(facesContext, currentClientId).getString(messageDescriptor);
}
catch (MissingResourceException e)
{
return "???" + messageDescriptor + "???";
}
}
};
}
|
This example uses the existing message-context as template and creates a new context with a different message resolver.
In this example the client-id is used as resource-bundle name.
Code Block |
---|
title | Injecting the business client aware MessageContext |
---|
borderStyle | solid |
---|
|
@Inject
@ClientQualifier
private MessageContext clientAwareMessageContext;
|
To use such messages in a XHTML page, it's required to provide a bean which in-/directly implements the Map interface.
Code Block |
---|
title | Expose business client aware messages to XHTML pages |
---|
borderStyle | solid |
---|
|
@Named("clientMessages")
@Singleton
public class ClientAwareMessages extends MapHelper<String, String>
{
@Inject
@ClientQualifier
private MessageContext clientAwareMessageContext;
protected String getValue(String key)
{
return this.clientAwareMessageContext.message().text(key).toText();
}
}
|
Code Block |
---|
title | Usage in XHTML pages |
---|
borderStyle | solid |
---|
|
#{clientMessages.sample_message}
|
The full example is available in the current MyFaces CODI example application.
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
.
...