Primefaces commandButton无法正常工作

时间:2014-04-28 13:53:28

标签: java jsf primefaces

我希望,当我按下commandButton时,刷新我的页面(所有inputText框,所有checkBox都是空的,就像重新加载页面一样)。 我尝试过update =" @ requestForm",update =":requestForm",update =" @ all" ajax =" false",update =" @([id $ = requestForm])",但没有成功......

这是我的commandButton代码:

  <h:commandButton value="Save" id="saveBtn"
                        actionListener="#{requestBean.addRequest()}" ajax="false" />

我的表单有id =&#34; requestForm&#34;。有什么想法吗?

5 个答案:

答案 0 :(得分:1)

有很多方法可以实现它。

  1. 您可以使托管bean请求作用域。
  2. 提交后,您可以在辅助bean中将值设置为null
  3. 您可以使用resetValues组件的true(设置为p:ajax)属性。
  4. 您可以使用p:resetInput
  5. 中的p:commandButton组件
  6. 您可以在辅助bean中使用方法RequestContext.getCurrentInstance().reset("reference to the component to reset")
  7. 在Primefaces上有这些方法的例子 - ShowCase,在Misc下搜索ResetInput

答案 1 :(得分:0)

如果JSF当我们在输入字段中输入一些文本并且输入字段与back bean映射时,则UI-Page显示UI-Page上的支持bean值的当前值。

示例:如果您在java类中有字段firstName,并且您要按<p:inputText />设置名称并单击按钮Save,则该值为有效值,直到对象直播。 因此,在firstName方法中点击save按钮后,您必须在actionListener="#{requestBean.addRequest()}"中提供空值。 和 使用update =“@ form”。如果两者都不起作用,那么你必须为此编写JavaScripts。 如果不行,请告诉我。

答案 2 :(得分:0)

我不认为使用#id选择器更新了primefaces,请尝试使用样式类:

<h:form styleClass="form-selector"> 

<p:commandButton update="@(.form-selector)" />

答案 3 :(得分:0)

试试这些;

<p:commandButton  value="Clear" process="@this" type="reset" update="requestForm">
                 <p:resetInput target="requestForm"/>
 </p:commandButton> 

如果是对象,则需要将其设置为null并添加带有更新的ajax侦听器;

resetForm(){obj=null;}
 

然后;

<p:ajax event="change" update="requestForm" listener="${resetForm()}"/>

答案 4 :(得分:0)

这就是我必须这样做的 - 谁知道原因:

      <p:commandButton value="Reset" icon="ui-icon-refresh" process="@this" update=":form">
        <p:resetInput target=":form" clearModel="true" />
      </p:commandButton>

根据您的需要,可能不需要clearModel。请参阅关于resetInput的clearModel的PrimeFaces文档(默认为false):“启用时,重置输入还会将空值设置为绑定值,以便重置输入值和模型值。”

有人告诉我们here

  

以防其他人有同样的问题...

     

p:resetInput不接受@form作为目标。你必须提供   要清除的表单/命名容器的实际ID。一世   看过网上代码的例子(我想在StackOverflow上的某个地方)   使用@form作为目标 - 不要被愚弄,这不起作用。

这就是我使用target=":form"的原因,因为我的根NamingContainer是<h:form id="form">。有关NamingContainers选择内容here的大量信息。

另请注意,您可以先执行确认对话框:

      <p:commandButton value="Reset" icon="ui-icon-refresh" process="@this" update=":form">
        <p:resetInput target=":form" clearModel="true" />
        <p:confirm header="Confirm Reset" message="Are you sure you want to reset all changes?" icon="ui-icon-help" />
      </p:commandButton>

      <p:confirmDialog global="true" closable="true" closeOnEscape="true" showEffect="fade" hideEffect="fade">
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"  />
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
      </p:confirmDialog>