Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a JSF success message

Tags:

validation

jsf

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.

like image 706
Bart1990 Avatar asked Apr 17 '11 20:04

Bart1990


2 Answers

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);
like image 94
raghav Avatar answered Oct 23 '22 10:10

raghav


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.

like image 20
BalusC Avatar answered Oct 23 '22 12:10

BalusC