【问题标题】:Primefaces DialogFramework Growl and showMessageInDialog not workingPrimefaces DialogFramework Growl 和 showMessageInDialog 不起作用
【发布时间】:2014-01-20 11:09:14
【问题描述】:

我正在使用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. Dialog.xhtml 使用context.closeDialog(this); 关闭后更新Table.xhtml 的咆哮

  2. 在按下saveValue p:commandButton 之后但在关闭对话框之前使用context.showMessageInDialog(....) 显示一条消息。

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

我已经尝试了以上所有方法,但没有一个出现。如果我直接尝试运行Dialog.xhtmlDialog.xhtml 上的 p:growl 和context.showMessageInDialog(....) 都会很容易出现。

任何有关如何在使用 DialogFramework 时显示上述选项的帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: java ajax jsf primefaces dialog


    【解决方案1】:

    如果我理解正确,您希望在用户单击 saveValue 命令按钮后使用咆哮组件显示一条消息。

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

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

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

    在你的 saveValue 命令按钮更新如下:

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

    这种平静的代码足以将消息添加到您的视图中:

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

    【讨论】:

    • 非常感谢,它解决了这个问题。现在下一点是咆哮为 Table.xhtml 上的 this.attributeValue 提供了 null。你能建议一些方法来在咆哮中获得正确的属性值吗?
    • 我找不到那个问题!一旦您根据需要定义了 inputText,只有在 this.attributeValue 不为空(或为空)时才会调用 actionListener。你有没有从那个 System.out.println("this.attributevalue = " + this.attributeValue); 得到什么?
    • 非常感谢!你一直很有帮助。这是我自己的愚蠢错误导致它为空。在 saveValue() 我有 this.attributeValue = null;这样下次对话框打开时,inputText 中不存在上一个值。通过删除该语句,正确的值会显示在咆哮声中。再次感谢。
    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2015-10-08
    • 2014-04-30
    相关资源
    最近更新 更多