【发布时间】: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:growl 或context.showMessageInDialog(....) 来显示成功添加值的消息。为此,我有以下可能的选择:
在
Dialog.xhtml使用context.closeDialog(this);关闭后更新Table.xhtml的咆哮在按下
saveValuep:commandButton之后但在关闭对话框之前使用context.showMessageInDialog(....)显示一条消息。在按下
saveValuep:commandButton之后但在对话框关闭之前更新一个咆哮 pn Dialog.xhtml。
我已经尝试了以上所有方法,但没有一个出现。如果我直接尝试运行Dialog.xhtml,Dialog.xhtml 上的 p:growl 和context.showMessageInDialog(....) 都会很容易出现。
任何有关如何在使用 DialogFramework 时显示上述选项的帮助将不胜感激。提前致谢。
【问题讨论】:
标签: java ajax jsf primefaces dialog