Reputation: 1723
How do I able to fetch all the messages with SEVERITY is ERROR only. I tried:
Iterator<FacesMessage> messages = facesContext.getMessages(clientId);
while (messages.hasNext()){
if(messages.next().getSeverity().toString()=="ERROR 2")System.out.println(messages);
}
Is this th right way? It doesnot intercept messages with ERROR severity.
Any help would be highly appreciated.
Upvotes: 3
Views: 8180
Reputation: 1109865
The comparison is wrong. You cannot (reliably) compare Strings on its content with ==
. When comparing objects with ==
, it would only return true
if they are of the same reference, not value as you seem to expect. Objects needs to be compared with Object#equals()
.
But you can compare constants with ==
. The FacesMessage.Severity
values are all static constants. You should rather just compare Severity
with Severity
. Also the sysout is wrong, it is printing the iterator instead of the sole message.
This should work:
Iterator<FacesMessage> messages = facesContext.getMessages(clientId);
while (messages.hasNext()) {
FacesMessage message = messages.next();
if (message.getSeverity() == FacesMessage.SEVERITY_ERROR) {
System.out.println("Error: " + message);
}
}
Upvotes: 8