Search code examples
ajaxprimefacesjsf-2

How to keep JSF messages after redirect when ajax attribute is false on the PrimeFaces command Button


In JSF page ,The validations messages are displayed correctly, but it get disappeared suddenly. it happening when ajax attribute is set as false on the primefaces command button .

Below is the action method in the JSF managedBean

  FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getFlash().setKeepMessages(true);
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "error.nozipfile", null));
    return response().redirectPage(WEB_PAGE_LIST);

primefaces command button

<p:commandButton
          value="#{lang.save}"
          onclick="PF('importDialog').hide();
          PF('ajaxStatusDialog').show();"
          action="#{zcmsContentController.actionImport}"
          update="@this"
          ajax="false"
          process="@this"              
          styleClass="right-button"/>

How can i keep the messages even after redirecting ?


Solution

  • You can probably do this by putting faces messages in session scope, and on page load of the other page check if there are messages is session that need to be displayed. Something like this

    List<FacesMessage> messages = new ArrayList<FacesMessage>();
    messages.add(new FacesMessage(FacesMessage.SEVERITY_INFO, "error.nozipfile", null));
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
    session.put("facesMessages", messages);
    

    And in the method called on load of the other page

    if (session.get("facesMessages") != null) {
        List<FacesMessage> messages = (List<FacesMessage>)session.get("facesMessages");
        for (FacesMessage message : messages) {
            context.addMessage(null, message);
        }
    }