我有一个简单的表格,要求用户输入文本。然后,当用户单击链接时,将显示一个对话框,其中包含用户输入的值。我的第一个问题是对话框未显示。
另一个问题与更新有关。第一次显示该表单时,HTML代码正确,并且#{dialogBean.location}
的当前值为空。
然后,我单击链接,HTML代码为“错误”。这就是为什么我认为它没有显示出来的原因:
<form id="dialogForm" name="dialogForm" method="post" action="/tcmt-component/dialog.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="dialogForm" value="dialogForm">
<input type="hidden" autocomplete="off" value="-6424900608015567042:-9068630845666043913" id="javax.faces.ViewState" name="javax.faces.ViewState"></form>
同时,我检查Ajax调用的返回。
#{dialogBean.location}
的值仍为空。<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="dialogForm:dialog"><![CDATA[<div id="dialogForm:dialog" style="display:none" title="Dialog">
Current Location:
我再次单击链接,这次
#{dialogBean.location}
的值设置为正确的值。<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="dialogForm:dialog"><![CDATA[<div id="dialogForm:dialog" style="display:none" title="Dialog">
Current Location: MyLocation
bean :
@ManagedBean
@SessionScoped
public class DialogBean implements Serializable {
private String location;
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
}
风景:
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
</h:head>
<h:body>
<h:form id="initForm">
<h:inputText id="location" value="#{dialogBean.location}" />
<p:commandLink update="dialogForm:dialog" onclick="dlg.show()">
<h:outputText value="Show Dialog" />
</p:commandLink>
</h:form>
<h:form id="dialogForm">
<p:dialog id="dialog" header="Dialog" widgetVar="dlg" resizable="false">
Current Location: <h:outputText value="#{dialogBean.location}" />
<p:commandButton value="Close" oncomplete="dlg.hide();"/>
</p:dialog>
</h:form>
</h:body>
</html>
解决方案
似乎这是更新对话框的主要问题。相反,我将对话框包装在面板中,并且更新有效:
<p:dialog header="Dialog" widgetVar="dlg" resizable="false">
<p:outputPanel id="dialogPanel">
Current Location: <h:outputText value="#{dialogBean.location}" />
<h:form id="dialogForm">
<p:commandButton value="Close" oncomplete="dlg.hide();"/>
</h:form>
</p:outputPanel>
</p:dialog>
<h:form id="initForm">
<h:inputText id="location" value="#{dialogBean.location}" />
<p:commandLink update="dialogPanel" onclick="dlg.show()">
<h:outputText value="Show Dialog" />
</p:commandLink>
</h:form>
最佳答案
尝试将对话框放在命令链接之前,如下所示:
<p:outputPanel id="panel">
<h:form id="dialogForm">
<p:dialog id="dialog" header="Dialog" widgetVar="dlg" resizable="false">
Current Location: <h:outputText value="#{dialogBean.location}" />
<p:commandButton value="Close" oncomplete="dlg.hide();"/>
</p:dialog>
</h:form>
</p:outputPanel>
<h:form id="initForm">
<h:inputText id="location" value="#{dialogBean.location}" />
<p:commandLink update="dialogForm:dialog" onclick="dlg.show()" update="panel">
<h:outputText value="Show Dialog" />
</p:commandLink>
</h:form>
另一个简单的解决方案是:如果使用Primefaces 3.0(或更高版本),则可以向对话框添加dymaic属性。将其设置为true。
这是primefaces 3.2的VDL
动态模式允许对话框在显示之前获取其内容,而不是在页面加载时获取,这对于减少初始页面加载时间很有用。默认为false。
关于JSF 2.0更新primefaces对话框第一次不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824705/