开发者

jsf message severity

开发者 https://www.devze.com 2022-12-21 09:18 出处:网络
How do I able to fetch all the messages with SEVERITY is ERROR 开发者_高级运维only. I tried: Iterator<FacesMessage> messages = facesContext.getMessages(clientId);

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.


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消