I was wondering how you can add a "success" message on a JSF page when the submitted data in a h:form is valid. e.g. "record successfully inserted". I tried using FacesContext.addMessage(String clientId, String message), but the message gets displayed in the same h:messages box that displays error messages in case of invalid data, resulting in text with a red color. I wish not to use JavaScript Alerts.
Any help would be greatly appreciated.
use different style class
h:messages infoStyle="color:darkgreen" errorStyle="color:darkred"
When adding message for success
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
for failure
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null);
Use null
as client ID to make it a global message and use the following in the view to display global messages only:
<h:messages globalOnly="true" />
Just put this somewhere below or above the form, depending on the functional requirement. If you're already using a <h:messages/>
to display validation errors instead of <h:message/>
for each input element, then you need to set globalOnly="false"
on this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With