1

I want, when i press the commandButton, to refresh my page (all inputText boxes, all checkBoxes to be empty, as if the page is reloaded. I tried with update="@requestForm", update=":requestForm", update="@all" ajax="false", update="@([id$=requestForm])", but no success...

This is the code for my commandButton:

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

My form has id = "requestForm". Any ideas?

4
  • please post more code, specifically the entire <h:form>...</h:form> edit* actually don't do that. I think jsf is smart enough to keep values in text boxes on a POST. In your backing bean method #{requestBean.addRequest()} you may need to reset all values to null / blank
    – j.con
    Commented Apr 28, 2014 at 13:59
  • you can also look at resetValues for <f:ajax>
    – j.con
    Commented Apr 28, 2014 at 14:05
  • First of all you are using h:commandButton. h:commandButton does not have a ajax attribute so no need to mention ajax="false". No need to mention empty parenthesis after method name if you are not passing parameters. Use action instead of actionListener if you don't have anything to do with the instance of the component which raised the event. Ideally you should have something like this <h:commandButton value="Save" id="saveBtn" action="#{requestBean.addRequest}" /> Commented Apr 28, 2014 at 14:06
  • Unfortunately, same result.
    – Tot
    Commented Apr 28, 2014 at 14:57

5 Answers 5

1

There are many ways to achieve it.

  1. You can make your managed bean request scoped .
  2. You can set the values to null in the backing bean , after submit.
  3. You can use resetValues (set to true) attribute of the p:ajax component.
  4. You can use the p:resetInput component inside your p:commandButton
  5. You can use the method RequestContext.getCurrentInstance().reset("reference to the component to reset") in your backing bean.

There are examples of these methods on Primefaces - ShowCase, search for ResetInput under Misc.

1

This is how I had to do it - who knows why:

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

Depending on your needs, clearModel may not be necessary. See the PrimeFaces documentation on resetInput's clearModel (defaults to false): "When enabled, reset input also sets null values to the bound values so it reset inputs values and model values."

As someone tells us here:

Just in case someone else has this same problem...

p:resetInput does NOT accept @form as a target. You must supply the actual ID of the form/naming container that you would like to clear. I have seen examples of code online (I think somewhere on StackOverflow) that used @form as the target-- don't be fooled, this does not work.

This is why I used target=":form", because my root NamingContainer was <h:form id="form">. There's loads of information on the NamingContainers selection stuff here.

Also, note that you can do this to have a confirm dialog first:

      <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>
0

In case of JSF when we enter some text in input filed and that input filed is mapped with back bean, then the UI-Page is showing the current value of backing bean value on UI-Page.

Example: if you have field firstName in java class and you are setting the name by <p:inputText /> and click on button Save the value is live until object live. so you have to provide null value in firstName after clicking save button in method of actionListener="#{requestBean.addRequest()}". And use update="@form". if both is not work then you have to write JavaScripts for that. if not work let me know.

2
  • I tried everything written above, but the result is still the same. <h:commandButton value="Save" id="saveBtn" actionListener="#{requestBean.addRequest()}" update="@form"/> and putting set methods for all properties in the addRequest() method did not help.
    – Tot
    Commented Apr 28, 2014 at 14:55
  • @ankushyadav, There is no need for Javascript.
    – user2880020
    Commented Apr 28, 2014 at 18:44
0

I don't think primefaces update works with #id selector, please try with style class :

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

<p:commandButton update="@(.form-selector)" />
1
  • Do not work for me, i even did not manage to fix it with the above suggestion... that is why i created a new page and redirect to it after i submit the form.
    – Tot
    Commented Apr 30, 2014 at 17:58
0

Try these;

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

If it is an object, you need to set it to null and add ajax listener with update;

resetForm(){obj=null;}

Then;

<p:ajax event="change" update="requestForm" listener="${resetForm()}"/>
1
  • It says: "<p:ajax> Event:change is not supported." and where exactly i should put this line.
    – Tot
    Commented May 2, 2014 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.