16

hi im kind of new at jsf enviroment, im trying to update a primefaces growl and then redirect to a page from a commandButton action.

 <p:commandButton value="Guardar"  action="#{AgendamientoMBean.procesaAgendamientoJ()}" 
 update="growlDetalle" />  

The managed bean code its

   FacesContext context = FacesContext.getCurrentInstance();
   context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, descripcion, detalle));
   ....
   ....
    return "agp_bandeja_citas_llamadas?faces-redirect=true";

This only redirectme to the page but doesnt show me the growl message, i tested that if change my method to not return the page the message does show..

I was trying to update the growl of the page that im redirecting but thats impossible i guess.

i think that when redirecting i lost the update functionality because im in new page now.

Is there a way to tell jsf to first do the update and then redirecting?

Hope you can help me, thanks in advance

arkantos
  • 457
  • 4
  • 6
  • 13

3 Answers3

51

Messages get lost during redirect. You can use the flash to keep messages.

Add the following before returning from your action method:

FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true);
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • 8
    Note that this approach has a peculiar bug when used with Mojarra: it won't work then the redirect goes to a different path. If the current and target page are in the same path (the same folder in URL), then it will work fine. See also http://java.net/jira/browse/JAVASERVERFACES-1877 – BalusC Mar 29 '12 at 20:26
1

Add another growl to page you redirect

like this <p:growl id="growlmsg2" showDetail="true" sticky="true" autoUpdate="true"/>

FreeBird
  • 11
  • 1
-1

Because Flash has bug, my solution is to 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