ajax – 如何使用PrimeFaces消息组件堆叠消息?

我有一个关于p:messages组件的问题.

首先,这是我的配置:

> PrimeFaces:4.0.3(精英)
> JSF:MyFaces 2.0.2
>服务器:WebSphere 8.5.0.2

然后,我的代码

test.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:p="http://primefaces.org/ui"
   xmlns:fn="http://java.sun.com/jsp/jstl/functions">

<h:head>
   <f:facet name="first">
       <Meta http-equiv="X-UA-Compatible" content="IE=Edge" />
       <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   </f:facet>
   <Meta http-equiv="cache-control" content="no-store,no-cache" />
   <Meta http-equiv="pragma" content="no-cache" />
   <Meta http-equiv="expires" content="0" />
</h:head>

<h:body>

   <div id="content">     
       <h:form id="form1"> 
           <p:tooltip/>
           <p:messages id="messages" showDetail="true" />
           <p:remoteCommand async="true" autoRun="true" process="@this" partialSubmit="true" action="#{testBean.checkA}" />
           <p:remoteCommand async="true" autoRun="true" process="@this" partialSubmit="true" action="#{testBean.checkB}" />
       </h:form>  
   </div>

</h:body>

</html>

Test.java

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

@ManagedBean(name="testBean")
@ViewScoped
public class Test implements Serializable {
    private static final long serialVersionUID = -1L;

    public void checkA() {
       try {
         Thread.sleep(2000);
       } catch (InterruptedException e) {
         e.printstacktrace();
       }
       FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,"Message 1",null));
       RequestContext.getCurrentInstance().update("form1:messages");
    }  

    public void checkB() {
        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Message 2",null));
        RequestContext.getCurrentInstance().update("form1:messages");
   } 
}

这段代码的作用非常简单.页面加载时,会进行两次AJAX调用(checkA和checkB).我在checkA中放了一个Thread.sleep(2000)用于测试目的,因为我希望它在checkB之后完成.方法完成后,会向UI发回消息.

当我加载页面时,我看到两个AJAX调用. CheckB将首先完成,所以我会在我的页面上看到“消息2”.但是一旦checkA完成,消息将被“消息1”替换.我想做的是将“消息1”附加到“消息2”,以便用户在屏幕上看到这两条消息.有没有办法做到这一点,还是组件的限制?

其次,正如你在我的代码中看到的,我必须调用RequestContext.getCurrentInstance().update(“form1:messages”);为了看到我的p:消息正在更新.如果我删除这行代码并将autoUpdate =“true”添加到组件,它不起作用…

有一件事需要考虑,遗憾的是,我无法更改服务器的配置,因为我没有管理它,并且已经有很多其他应用程序.

在此先感谢您的帮助!

在这里遇到的问题是FacesMessages是 Request Scoped.当你执行两个ajax请求时,当你到达第二个请求时,第一个消息就不可用了.

你正在使用一个@ViewScoped bean,它一直存在,直到你离开视图本身,所以解决方法应该是有一种堆栈/队列存储要显示的消息.当您考虑完成最后一个请求时,只需将所有消息添加到FacesContext并清除堆栈.

其他选择是以相同的方法执行所有操作,并在此之后显示所需的消息.你会因为执行一个请求而不是两个请求而减少网络流量,但如果你想要render specific parts of the page before other ones,你的选择似乎很有意思.

关于更新问题,请记住您可以直接在页面中指定要更新的表单部分.尝试将update =“messages”添加到p:remoteCommand标记中.关于autoUpdate不工作,可能你的remoteCommand的partialSubmit属性可能会带来你的问题.从Primefaces docs查看该属性的定义:

partialSubmit: Enables serialization of values belonging to the partially processed components only.

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...