duminică, 17 mai 2015

[OmniFaces utilities 2.0] Create/add a parameterized global (or to the given client ID) ERROR faces message


[OmniFaces utilities] The createError() method create an ERROR faces message with the given message body which is formatted with the given parameters.
[OmniFaces utilities] The addError() method adds an ERROR faces message to the given clientId, with the given message body which is formatted with the given parameters.

Method Messages#createError() - Create an ERROR faces message with the given message body which is formatted with the given parameters

Method Messages#addError()- Add an ERROR faces message to the given clientId, with the given message body which is formatted with the given parameters
See also: Faces#getContext()
Usage:

private static final String FILES_UPLOADED_ERROR = "The {0} file cannot be uploaded ! File size should be smaller than {1} bytes.";

import org.omnifaces.util.Messages;
...
// for a certain component, replace null with clientId
FacesMessage error = Messages.createError(FILES_UPLOADED_ERROR, "order.txt", 1024);
// add the error FacesMessage to the current list of messages, or/and do some something else with it
Messages.add(null, error);
     
// or, if you just want to add it in the messages list, use directly Messages#addError()
// for a certain component, replace null with clientId
Messages.addError(null, FILES_UPLOADED_ERROR, "order.txt", 1024);

Output:
The order.txt file cannot be uploaded ! File size should be smaller than 1,024 bytes.

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, <h:message>, for component messages, or any other approach capable to display the messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

Niciun comentariu:

Trimiteți un comentariu