2

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

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

Community
  • 1
  • 1
Abaddon666
  • 1,533
  • 15
  • 31
0

Because according to the docs PrimeButton showcase, the button by default is Ajax, so you need to put the update param with the view you want to update, or add the param ajax="false"

 <p:commandButton value="Ajax Submit" id="ajax" update="growl" actionListener="#{buttonView.buttonAction}" styleClass="ui-priority-primary" />
 <p:commandButton value="Non-Ajax Submit" id="nonAjax" actionListener="#{buttonView.buttonAction}" ajax="false" />

Notice how the first option has an update attribute, or the second one an ajax="false"

rekiem87
  • 1,565
  • 18
  • 33