I am trying to display simple messages with <p:messages/>
but only I've got is the message of development mode of MyFaces. I will explain.
On my XHTML page, I have the following code to diplay my messages:
<div class="messagePanel">
<p:messages id="msgCalls"
for="msgCalls"
showDetail="true"
showSummary="true"
autoUpdate="true"
closable="true" />
</div>
On my bean:
@PostConstruct
public void init() {
try {
this.linhaCelularId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("linhaCelularId");
if (this.linhaCelularId == null || this.linhaCelularId.trim().isEmpty()) {
Messages.addGlobalError(MENSAGEM_GLOBAL, "Nenhum celular especificado para listagem de ligações");
}
else {
this.linhaCelularTitular = getLinhaCelularTitularService().getById(Long.parseLong(this.linhaCelularId));
if (this.linhaCelularTitular == null) {
Messages.addGlobalError(MENSAGEM_GLOBAL, "Celular não encontrado!");
}
else if (!this.linhaCelularTitular.getResponsavel().getAn8().equals(this.loginMB.getLoggedUser().getAn8())) {
Messages.addGlobalError(MENSAGEM_GLOBAL, "Você não tem permissão para acessar essa página. Esse erro foi reportado.");
}
else {
this.faturaTitular = getFaturaTitularService().getUltimaFaturaCarregada();
this.itensFaturaTitular = getFaturaTitularService().getItensFaturaPorCelularFatura(this.linhaCelularTitular, this.faturaTitular);
}
}
}
catch (ServiceException e) {
logger.error("Erro ao buscar ultima fatura carregada", e);
throw new RuntimeException(e);
}
}
The problem is, that the message added by Faces.addGlobalError()
or Faces.addError()
is not showing by my <p:messages.../>
. The message only shows up on Unhandled Messages of development mode. Take a look when I inspect the HTML:
<ul id="javax_faces_developmentstage_messages"
title="Project Stage[Development]: Unhandled Messages"
style="color:orange">
<li>
<span title="Project Stage[Development]: Unhandled Messages">Você não tem permissão para acessar essa página. Esse erro foi reportado.</span>
</li>
</ul>
I already tried some other ways to do:
<p:messages/>
and <h:messages>
on XHTML;Faces.addGlobalError()
and Faces.addError()
;FacesMessage message = new FacesMessage();
None of above worked. I don't know what else to do. Currently, I am using MyFaces 2.1.12, Primefaces 4.0 and Omnifaces 1.5.
Found the solution.
When I access my XHTML with GET request and parameters, the @PostConstruct
is not called until "some reference" to the managed bean on page is made. So, the tag <p:messages />
was placed before the first reference to the managed bean. When the @PostConstruct
was called, it was already too late to render the messages.
The solution is to put the a preRenderView
before the tag <p:messages />
and point to a method. Even to point to an empty method. Tthe managed bean will be constructed before the <p:messages />
and all messages will be rendered.
Here is a nice explanation about this topic by @BalusC