为什么我的 JSF 应用程序不显示上下文消息?

Why my JSF application doesn't show the context message?

我正在使用 Prime Faces,我希望用户从索引页面开始,按下按钮调用函数 (registrarUsuario) 以重新加载索引页面并显示上下文消息。我的问题是我的应用程序没有显示消息。 这是我的代码:

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");
    }

}

谢谢!

发生这种情况是因为 Primefaces 不通过重定向保留消息。

解决这个问题的一种可能性是添加一个过滤器,将所有未呈现的消息传递到重定向之后。这个答案提供了一个解决方案:Show success message and then redirect to another page after a timeout using PageFlow

因为根据文档PrimeButton showcase,按钮默认是Ajax,所以你需要把update参数和你想更新的视图放在一起,或者添加参数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" />

注意第一个选项如何具有 update 属性,或者第二个选项如何具有 ajax="false"