Primefaces DialogFramework Growl和showMessageInDialog不起作用

时间:2014-01-20 11:09:14

标签: java ajax jsf primefaces dialog

我正在使用primefaces dialogFramework。我有一个Table.xhtml文件,如下所示

<h:form id="form">
    <p:growl id="growl" showDetail="true" sticky="true" />
    <p:dataTable id="colors" var="col" value="#{tableDialog.resourceList}" rowKey="#{col}"  
                 selection="#{tableDialog.selected}" selectionMode="single">  
        <p:column headerText="Model">#{col}</p:column>  
    </p:dataTable>  
    <p:contextMenu for="colors">
        <p:menuitem  value="Add"  onclick="triggerHiddenEvent(); return false;" update=":form:colors" />
    </p:contextMenu>

    <p:commandButton id="hiddenCommand" styleClass="button"  action="#{tableDialog.updateValue}"  style="display:none">
        <p:ajax event="dialogReturn"  update = ":form:colors :form:growl " action="#{tableDialog.showValue}" actionListener="#{tableDialog.showValue}"  />  
    </p:commandButton>

    <h:outputScript >
        function triggerHiddenEvent() {
        document.getElementById("form:hiddenCommand").click();
        }
    </h:outputScript>
</h:form>

和相应的ManagedBean如下

@ManagedBean
@SessionScoped
public class TableDialog {

    public ArrayList<String> resourceList = new ArrayList<String>();

    private String selected;

    String attributeValue = null;

    public TableDialog() {
        this.resourceList.add("Black");
        this.resourceList.add("White");

    }

    public void updateValue() {
        System.out.println("update  value");
        RequestContext context = RequestContext.getCurrentInstance();
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("resizable", false);
        options.put("dynamic", true);
        options.put("height", 100);
        options.put("width", 300);
        options.put("contentHeight", 100);
        options.put("contentWidth", 250);
        context.openDialog("Dialog", options, null);
    }

    public void cancelValue() {
        RequestContext context = RequestContext.getCurrentInstance();
        context.closeDialog(this.attributeValue);
        System.out.println("cancel update resource attribute value");
        this.attributeValue = null;
        System.out.println("this.attributevalue  = " + this.attributeValue);

    }

    public void saveValue() {
        RequestContext context = RequestContext.getCurrentInstance();
        if (this.attributeValue == null) {
            System.out.println("No value");
            context.execute("noValueDialog.show()");
            return;
        }
        System.out.println("this.attributevalue = " + this.attributeValue);
        this.resourceList.add(this.attributeValue);
        context.update("form:resourceAttributeValueDataTable");
        RequestContext.getCurrentInstance().update("form:resourceAttributeValueDataTable");
        //FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Hello " + this.attributeValue));
        context.showMessageInDialog(new FacesMessage("Failure", "good bye " + this.attributeValue));
        this.attributeValue = null;
        context.closeDialog(this.attributeValue);
        System.out.println("after hidden button execute ");
    }

    public String getSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public ArrayList<String> getResourceList() {
        return resourceList;
    }

    public void setResourceList(ArrayList<String> resourceList) {
        this.resourceList = resourceList;
    }

    public String getAttributeValue() {
        return attributeValue;
    }

    public void setAttributeValue(String attributeValue) {
        this.attributeValue = attributeValue;
    }

    public void showValue() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("successful", "hello " + this.attributeValue));
        System.out.println("showvalue from action ");
    }

    public void showValue(ActionEvent actionEvent) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("successful", "hello " + this.attributeValue));
        System.out.println("showvalue from actionlistener ");
    }
}

Table.xhtml打开Dialog.xhtml,如下所示

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Dialog</title>
    </h:head>

    <h:body>  
        <h:form>
            <p:growl id="growl" showDetail="true"  />

            <h:panelGrid id="updateValuePanel" columns="2" style="margin-bottom:10px">  
                <h:outputLabel value="Attribute Value "  />
                <p:inputText id="attributeValue" value="#{tableDialog.attributeValue}" required="true" />
            </h:panelGrid>  

            <p:commandButton id="saveValue" value="Submit" actionListener="#{tableDialog.saveValue}"  update="growl" />
            <p:commandButton id="cancelValue" value="Cancel " action="#{tableDialog.cancelValue}"/>
            <p:defaultCommand target="saveValue" />
        </h:form>
    </h:body>
</html>

我的问题/问题:

Dialog.xhtml用于添加将在Table.xhtml上显示的其他值。我想使用p:growlcontext.showMessageInDialog(....)来显示已成功添加值的消息。为此,我有以下可能的选择:

  1. 使用Table.xhtml

  2. 关闭Dialog.xhtml后,context.closeDialog(this);更新咆哮声
  3. 在按下context.showMessageInDialog(....) saveValue之后但在对话框关闭之前,使用p:commandButton显示消息。

  4. 在按下saveValue p:commandButton之后但在关闭对话框之前更新一个growl pn Dialog.xhtml。

  5. 我已经尝试了以上所有但是没有出现。如果我直接尝试在Dialog.xhtml上运行Dialog.xhtml p:growl,并且context.showMessageInDialog(....)两者都很容易显示。

    有关如何在使用DialogFramework时显示上述选项的任何帮助都将受到高度赞赏。提前致谢。

1 个答案:

答案 0 :(得分:4)

如果我理解正确,您需要在用户点击 saveValue commandButton后使用growl组件显示消息。

在这种情况下,我会在Table.xhtml的表单之外使用单个咆哮。

<p:growl id="growl" showDetail="true"/>

注意:考虑将autoUpdate属性定义为true( autoUpdate =“true”)。

在你的saveValue commandButton更新中,如下所示:

<p:commandButton id="saveValue" value="Submit" actionListener="#{tableDialog.saveValue}"  update=":growl" />

这段代码的安静性足以为您的观点添加消息:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Hello " + this.attributeValue));