【问题标题】:Growl message does not show up when created in method triggered by <p:commandButton async="true">在由 <p:commandButton async="true"> 触发的方法中创建时不显示咆哮消息
【发布时间】:2019-08-27 07:26:36
【问题描述】:

我目前正在创建一个带有 spring boot 和 primefaces 的小应用程序并且遇到问题,如果出现问题,我无法在我的网站中创建错误消息。 这是xhtml的一些代码:

<h:form id="form">
<p:growl id="msgs" showDetail="true"/>
...
<p:commandButton id="btnStartManualCopy" styleClass="pollUpdate" async="true" value="Start" disabled="#{daqScriptController.isProcessing()}" action="#{daqScriptController.startManualCopy}" />
</h:form>

请注意 async 为真,因此当调用的方法“startManualCopy”正在处理时,页面不会阻塞。
在“startManualCopy”方法中,如果在处理过程中出现任何错误并希望在页面上显示指定的消息 - 在咆哮声中,我也会处理错误。
到目前为止我所尝试的:

FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error:", "Some cool text); FacesContext.getCurrentInstance().addMessage(null, msg);

或:

PrimeFacesContext.getCurrentInstance().addMessage(null, msg);
PrimeFaces.current().ajax().update(":msgs");

或:

final RequestContext context = RequestContext.getCurrentInstance();
context.update(":msgs");

注意:我为 id 尝试了其他一些组合,例如 :form:msgs 等等 - 所以我可以确定正确的组件被寻址。但在网络观察者中,响应从来没有真正更新过。

我在调试时发现,异步调用方法内的 facescontext 中的响应者为空。我添加了一个侦听器,它在 tabclose 事件的咆哮声中添加一条消息,这工作得很好。

public void onTabClose(TabCloseEvent event) {
    FacesMessage msg = new FacesMessage("Tab Closed", "Closed tab: " + event.getTab().getTitle());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

那么知道如何显示我的用例中的错误消息吗?

谢谢

【问题讨论】:

标签: jsf primefaces


【解决方案1】:

您需要在 commandButton 中使用update="msgs" 来告诉它在 AJAX 返回后更新 msgs 组件。

<p:commandButton async="true" 
    value="Start"  
    action="#{daqScriptController.startManualCopy}" 
    update="msgs" />

【讨论】:

  • 不使用异步时,问题中的所有这些都不会失败吗?由于缺少更新不会更新任何内容? (我被异步部分误导了)
  • 谢谢.. 完成了这项工作。但我很确定我在第一次尝试时就做到了,但似乎我还有一个小错误。
  • @white91wolf 所以很可能它与异步无关。下次做题的时候要注意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
相关资源
最近更新 更多