01: package nl.knowlogy.validation.jsf;
02:
03: import java.util.Locale;
04: import java.util.ResourceBundle;
05:
06: import javax.faces.application.Application;
07: import javax.faces.context.FacesContext;
08: import javax.faces.el.ValueBinding;
09:
10: import nl.knowlogy.validation.Messages;
11:
12: public abstract class MessagesUtil {
13:
14: public static final String VALUE_BINDING_NAME = "#{requestScope.errors}";
15:
16: /**
17: * Adds messages to the facesContext, so displaying elements can show this
18: * messages.
19: *
20: * @param facesContext
21: * to which the messages are added.
22: * @param messages
23: * messages to add.
24: */
25: public static void addErrorsToFacesContext(
26: FacesContext facesContext, Messages messages) {
27:
28: Application application = facesContext.getApplication();
29: String messageResourceBundle = application.getMessageBundle();
30: if (messageResourceBundle != null) {
31:
32: Locale locale = facesContext.getViewRoot().getLocale();
33:
34: ResourceBundle resourceBundle = ResourceBundle.getBundle(
35: messageResourceBundle, locale);
36: messages.convertMessageCodes(resourceBundle);
37:
38: }
39: ValueBinding valueBinding = facesContext.getApplication()
40: .createValueBinding(VALUE_BINDING_NAME);
41:
42: // Messages existingMessages = (Messages) valueBinding.getValue(facesContext);
43: // if (existingMessages != null) {
44: // existingMessages.addMessages(messages);
45: // } else {
46: valueBinding.setValue(facesContext, messages);
47: //}
48:
49: }
50:
51: }
|