有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Primefaces对话框框架咆哮和showMessageInDialog不工作

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

  2. 在按下saveValuep:commandButton之后但在关闭对话框之前,使用context.showMessageInDialog(....)显示消息

  3. 更新咆哮pn对话框。在按下saveValue{}之后但在关闭对话框之前的xhtml

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

对于在使用DialogFramework时如何显示上述选项的任何帮助,我们将不胜感激。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

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

    在这种情况下,我会在桌子上的表格外发出一声咆哮。xhtml

    <p:growl id="growl" showDetail="true"/>
    
    <>强>注释:< /强>考虑将AutoPoDead属性定义为true(<强> AutoPoTe=“true”< /Sturt>)。

    在saveValue命令按钮上,按如下方式更新:

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

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

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