如何重置JSF Primefaces中的inputText字段
问题描述:
即使在调用show()之前将其设置为空白,以下对话框中的InputText字段仍会保留以前的值。当第一次调用show()时,inputText字段仅显示为空白。我的bean是会话范围。如何重置JSF Primefaces中的inputText字段
<p:dialog id="dlgId" widgetVar="dlgVar" dynamic="true">
<h:form>
<h:panelGrid columns="1">
<h:outputLabel for="nametext" value="Name" />
<p:inputText id="nametext" value="#{myBean.name}" />
</h:panelGrid>
<p:commandButton value="Save" actionListener="#{myBean.saveAction}" />
</h:form>
public void add(TreeNode selectedTreeNode) {
setName("");
RequestContext.getCurrentInstance().execute("PF('dlgVar').show()");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
我怎样才能获得inputTEext字段以显示我打电话展()而不是值先前由用户输入之前设置的价值?
答
问题是:您需要更新您的表单。要做到这一点,你可以使用这些解决方案之一。
解决方案1:从您的XHTML
<h:form id="form">
<h:panelGrid columns="1">
<h:outputLabel for="nametext" value="Name" />
<p:inputText id="nametext" value="#{myBean.name}" />
</h:panelGrid>
<p:commandButton value="Save" actionListener="#{myBean.saveAction}" update=":form" />
</h:form>
解决方案2更新:从您managedBean
YourXhtml更新
<h:form id="form">
...
</h:form>
YourManagedBean
public void saveAction() {
...
name = "";
RequestContext.getCurrentInstance().update(":form");
}
您还可以阅读本帖子Can I update a JSF component from a JSF backing bean method?。
解决方案3:使用一个Ajax事件
更新它,你也可以添加一个Ajax事件
<p:commandButton value="Save" type="button" >
<p:ajax event="click" listener="#{myBean.saveAction}" update=":form"/>
</p:commandButton>
我想您的两个以上建议,它仍然没有工作。让我进一步解释: 显示的第一次对话框:InputText字段为空,并且用户输入“Mr Smith” 第二次显示的对话框:InputText字段是“Mr Smith”,而不是空白,这意味着用户必须手动清除“Mr Smith”在输入名称之前。 – amah
您必须在哪里更新对话框1.当您打开对话框时或2.当您单击saveAction?!时? –
请看看我的编辑 –