如何在JSF Primefaces中重置inputText字段

问题描述:

InputText字段仍保留先前的值.仅当首次调用show()时,inputText字段才会显示为空白.我的bean是会话作用域的.

InputText field in the following dialog retains previous value even though I set it to blank before calling show(). The inputText field is only displayed blank when show() is called for the first time. My bean is session scoped.

<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字段以显示我在调用show()之前设置的值,而不是用户先前输入的值?

How can I get the inputTEext field to display the value I set before calling show() rather then the value previously entered by the user?

问题是:您需要更新表单.为此,您可以使用以下解决方案之一.

The thing is: you need to update your form. To make it, you can use one of these solutions.

解决方案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");
}

您也可以阅读此帖子我可以通过JSF支持bean方法更新JSF组件吗?.

You can also read this post Can I update a JSF component from a JSF backing bean method?.

解决方案3:使用Ajax事件对其进行更新

您还可以添加一个ajax事件

You can also add an ajax event

<p:commandButton value="Save" type="button" >
    <p:ajax event="click" listener="#{myBean.saveAction}" update=":form"/>
</p:commandButton>