加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:36:03 所属栏目:百科 来源:网络整理
导读:我有一个关于p:messages组件的问题. 首先,这是我的配置: PrimeFaces:4.0.3(精英) JSF:MyFaces 2.0.2 服务器:WebSphere 8.5.0.2 然后,我的代码: test.xhtml !DOCTYPE htmlhtml xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/
我有一个关于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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读