【问题标题】:Why my JSF application doesn't show the context message? [duplicate]为什么我的 JSF 应用程序不显示上下文消息? [复制]
【发布时间】:2016-11-15 07:57:49
【问题描述】:

我正在使用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");
    }

}

谢谢!

【问题讨论】:

    标签: jsf redirect message


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这是因为 Primefaces 不通过重定向保留消息。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-23
        • 2015-11-08
        • 2010-12-28
        • 1970-01-01
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        相关资源
        最近更新 更多