如何使用Primefaces的p:growl并重定向到页面

16

你好,我在JSF环境中有些新,我尝试更新一个PrimeFaces Growl,并从commandButton操作中重定向到一个页面。

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

这个托管的 Bean 代码

   FacesContext context = FacesContext.getCurrentInstance();
   context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, descripcion, detalle));
   ....
   ....
    return "agp_bandeja_citas_llamadas?faces-redirect=true";
这只是将我重定向到页面,但不显示 growl 消息,我测试了一下,如果更改我的方法以不返回页面,则消息会显示。
我试图更新我重定向的页面的 growl,但我想这是不可能的。
我认为当进行重定向时,我丢失了更新功能,因为现在我在新页面上。
有没有办法告诉 jsf 先执行更新,然后再重定向?
希望你能帮助我, 提前感谢。
3个回答

51

重定向过程中可能会丢失消息。您可以使用flash来保留消息。

在您的动作方法返回之前添加以下内容:

FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true);

8
注意,当使用Mojarra时,这种方法有一个奇怪的bug:当重定向到不同路径时它将无法工作。如果当前页面和目标页面在同一路径下(URL中的同一文件夹),那么它将正常工作。另请参阅http://java.net/jira/browse/JAVASERVERFACES-1877。 - BalusC

1
在你重定向的页面上添加另一个提示框,就像这样: <p:growl id="growlmsg2" showDetail="true" sticky="true" autoUpdate="true"/>

-1
由于Flash存在Bug,我的解决方案是创建一个单独的重定向按钮,在显示消息后点击该按钮:
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.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接