我正在尝试使用弹出式对话框编写JSF页面,该对话框允许用户输入数据。我之前使用过PrimeFaces,但出于各种原因,我决定不将它用于这个项目。毕竟,我只需要一个简单的弹出对话框。自己编码有多难?
假设的工作方式是:
我的问题是当用户按下Save并获得验证错误然后按Cancel时,对话框不会消失。我可以看到,在这种情况下,inputHidden字段的值 not 更新为false。支持bean上的isShowDialog()方法 not 被调用。
这是我的面孔:
<?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">
<h:head>
<h:outputScript library="js" name="jquery-1.11.0.js" />
</h:head>
<h:body>
<h:messages id="pageMessages" errorStyle="color: red;" infoStyle="color: green;" />
<script type="text/javascript">
function showHideDialog(data) {
if (data.status == "success") {
if ($("#dialogForm\\:showDialog").val() == "true") {
$("#dialogDiv").show();
$("#pageMessages").hide();
} else {
$("#dialogDiv").hide();
$("#pageMessages").show();
}
}
}
</script>
<div id="dialogDiv" style="border: thick solid black; display: none;">
<h:form id="dialogForm">
<h:messages id="dialogMessages" errorStyle="color: red;" infoStyle="color: green;" />
<h:inputHidden id="showDialog" value="#{backingBean.showDialog}" />
<h:panelGrid columns="2">
<h:outputLabel for="dialogField" value="Some Field:" />
<h:inputText id="dialogField" value="#{backingBean.dialogField}" label="Some field" required="true" />
</h:panelGrid>
<h:commandButton value="Save" action="#{backingBean.save}">
<f:ajax event="action" execute=":dialogForm" render=":dialogForm :pageMessages" onevent="showHideDialog" />
</h:commandButton>
<h:commandButton value="Cancel" action="#{backingBean.cancel}" immediate="true">
<f:ajax event="action" render=":dialogForm :pageMessages" onevent="showHideDialog" />
</h:commandButton>
</h:form>
</div>
<h:form id="pageForm">
<p>Normal page content. Blah, blah, blah.</p>
<h:commandButton value="Add New Record" action="#{backingBean.addNew}">
<f:ajax event="action" render=":dialogForm :pageMessages" onevent="showHideDialog" />
</h:commandButton>
</h:form>
</h:body>
</html>
这是我的支持bean:
package com.mycompany.example;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class BackingBean {
private String dialogField;
private boolean showDialog = false;
public String addNew() {
dialogField = null;
showDialog = true;
return null;
}
public String save() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "New row successfully saved.", null));
showDialog = false;
return null;
}
public String cancel() {
showDialog = false;
return null;
}
public String getDialogField() { return dialogField; }
public void setDialogField(String dialogField) { this.dialogField = dialogField; }
public boolean isShowDialog() { return showDialog; }
public void setShowDialog(boolean showDialog) { this.showDialog = showDialog; }
}
如果重要,我正在使用Mojarra 2.2.5和Tomcat 7.0.42。
任何建议或见解都将不胜感激。
答案 0 :(得分:2)
我找到了 的答案。我改变了
<h:inputHidden id="showDialog" value="#{backingBean.showDialog}" />
到
<input id="dialogForm:showDialog" type="hidden" value="#{backingBean.showDialog}" />
答案 1 :(得分:0)
我找到了以下BalusC文章的答案:Okay, when should I use the immediate attribute?
简而言之,请使用
<{1}}中的 immediate="true"
,
和
<h:inputHidden id="showDialog"
内execute="showDialog"
内的{p> <f:ajax
。
P.S。:您的示例代码非常有用。