Tuesday, August 24, 2021

JEE: Using JSF, Primefaces and p:growl the messages return duplicated (twice).

 

>PROBLEM

Using JSF, Primefaces and p:growl the messages return duplicated (twice).





This kind of issue may have alternative reasons.
On this post it is shown two of them:
- The simple case
and
- The modal case


>SOLUTION #1 - SIMPLE CASE

Find the p:grow element.

Switch the p:growl attribute from:

<p:growl id="growl" showDetail="true"/>

to:

<p:growl id="growl" showDetail="false"/>


Supposing the java code is something like this:

public String remove() {
FacesMessage success = null;
try {
bsn.delete(toEntity());
success = new FacesMessage(FacesMessage.SEVERITY_INFO, "SUCCESS: ID " + this.id + " removed", null);
FacesContext.getCurrentInstance().addMessage(null, success);
clean();
return ModelConfigSvc.SUCCESS;
} catch (BsnException e) {
success = new FacesMessage(FacesMessage.SEVERITY_ERROR, "deletion failed",null);
FacesContext.getCurrentInstance().addMessage(null, success);
return ModelConfigSvc.FAIL;
}
}


or
public String remove() {
FacesMessage success = null;
try {
bsn.delete(toEntity());
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "SUCCESS: ID " + this.id + " removed", null));
clean();
return ModelConfigSvc.SUCCESS;
} catch (BsnException e) {
success = new FacesMessage(FacesMessage.SEVERITY_ERROR, "deletion failed",null);
FacesContext.getCurrentInstance().addMessage(null, success);
return ModelConfigSvc.FAIL;
}
}






>SOLUTION #2 - MODAL CASE

Scenario:
There is a page that lists entities and it may trigger modal pages to update, delete, etc.
In such cases, the modal message may aggregate to the origin page's message, in this case the listing page.

A fast way to fix this leads us to switch the return value to keep the modal opened and not returning to the listing page, so the message doesn't accumulate twice, one from the modal (p:messages) and the other from the listing page (p:growl).

So, it is changed from:
return ModelConfigSvc.SUCCESS;

To:
return ModelConfigSvc.FAIL;

Below, it is shown the detailed information using the "deletion" to illustrate the example.






Issue to be fixed - doubled messages when the modal is closed after the deletion





THE CODE

faces-config.xhtml



roleList.xhtml





roleDeletion.xhtml exibted as modal







FIXING THE ISSUE


RoleDeletion.java







Return value switched to fix the issue

RoleDeletion.java 





Issue fixed






>ENV


Windows 10
JSF 2.2
WildFly 21





No comments:

Post a Comment

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...