I am working with Prime Faces
and I would like user start in a index page, press a button for call a function (registrarUsuario
) for reload the index page and show the context message. My problem is my application doesn't show the message.
This is my code:
index.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:b="http://bootsfaces.net/ui">
...
<h:form>
<b:commandButton action = "#{registroUsuario.registrarUsuario}" value = "call function"/>
</h:form>
....
RegistroUsuario.java:
public void registrarUsuario() throws IOException, Exception {
try {
//another code
FacesMessage message = new FacesMessage(":D", "message");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
} catch (Exception e) {
FacesMessage message = new FacesMessage("Falla", e.toString());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
}finally{
FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
}
}
Thank you!
This happens because Primefaces does not preserve Messages through a redirect.
One possibility to cope with this is to add a filter which passes all unrendered messages to after the redirect. This answer provides a solution: Show success message and then redirect to another page after a timeout using PageFlow