1

I'm using primefaces 3.5 and I can't figure it out how to growl a message on the next page. For instance I want to add a record in database and after that I make a redirection to another page where I want to show a growl message with "The record has been added with success!" I tried something like this:

public String addLabelInDB() {
        try {
            //logic to add a record in DB

            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));

        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return "listLabelsPage";
    }

and in listLabelsPage.xhtml I have:

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

but it doesn't work. I supposed the message is getting lost because is another request or something? It's there any possibility to store the message on request and show it on the next page? Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Aditzu
  • 696
  • 1
  • 14
  • 25

3 Answers3

2

You can have a preRender set on the listLabelsPage.xhtml page you're loading

<f:event type="preRenderView" listener="#{yourBean.showGrowl}" />

and a showGrowl method having only

public void showGrowl() {
  FacesContext context = FacesContext.getCurrentInstance();
  context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));
}
Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • Although using your solution the message is always showed onload of listLabelsPage.xhtml it helps me a lot because setting a flag if the insertion was made with success I achieved what I what. – Aditzu Jan 06 '15 at 15:32
1

I post an answer to my own question in order to help another people which face the same problem like I did:

public String addLabelInDB() {
        try {
            //some logic to insert in db
            //below I set a flag on context which helps me to display a growl message only when the insertion was done with success
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            ec.getRequestMap().put("addedWithSuccess","true");

        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return "listLabelsPage";
    }

    public void showGrowl() {
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            String labelAddedWithSuccess = (String) ec.getRequestMap().get("addedWithSuccess");
           //if the flag on context is true show the growl message
            if (labelAddedWithSuccess!=null && labelAddedWithSuccess.equals("true")) {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));
            }
        }

and in my xhtml I have:

<f:event type="preRenderView" listener="#{labelsManager.showGrowl}" />
Aditzu
  • 696
  • 1
  • 14
  • 25
  • Not working ... externalContext dont have that object named "addedWithSuccess" on next page – Irfan Nasim May 06 '15 at 09:55
  • Is not on externalContext but on the request from the external context. Maybe you did another request in the meanwhile. This to put "addedWithSuccess" on session if on request is not working for you – Aditzu May 07 '15 at 11:38
0

How about this? Make a separated redirect button which will be hit after showing msg:

HTML:

<h:form prependId="false">
    <p:growl />
    <p:button outcome="gotoABC" id="rdr-btn" style="display: none;" />
    <p:commandButton action="#{bean.process()}" update="@form" />
</form>

Bean:

public void process(){
    addInfoMsg(summary, msgDetail); //Add msg func
    RequestContext.getCurrentInstance().execute("setTimeout(function(){ $('#rdr-btn').click(); }, 3000);"); // 3 seconds delay. I put the script in Constants to config later.
}
Quân Trần
  • 31
  • 1
  • 6