JSF immediate = true for cancel button not working

I have two tabs, one tab is a list of records, clicking on a record switches to the "Edit" tab, and on the "Edit" tab - "Save" and "Cancel" buttons.

Now I click on record No. 1, make some changes and click on the "Cancel" button. Of course, I do not want to validate the form because it is canceled, so I set immediate="true" to the "Cancel" button. Now the editing tab is closed, back to the list of entries. Then I click on another record No. 2, there was a problem: on the editing tab, it is still the previous contents of record No. 1, and not record No. 2. I checked the variables in debug mode, the inverse of the bean for the edit form was ACTUALLY populated with record # 2.

Ie, something is broken after an immediate command.

(Everything was long before I added confirmation and immediately = "true".)

 class FormBean { Record activeRecord; ... public void clickOnList() { activeRecord = loadRecord(clickIndex); } public void cancelForm() { activeRecord = null; } } 

page.xhtml:

 <h:form id="main"> ... <p:tab title="Edit" rendered="#{formBean.activeRecord != null}"> ... <p:commandButton value="Cancel" actionListener="#{formBean.cancelForm}" update="main" async="true" immediate="true" /> </p:tab> </h:form> 
+4
source share
1 answer

immediate="true" does not work with ajax requests on the same view. This boils down to the fact that the presented but not confirmed values ​​are still present in the input components. Without ajax, a regular synchronous request / response of a subsequent form will silently "solve" this. But with ajax this does not happen. The presented values ​​of the previous ajax request are still present in the input components. If UIInput#getSubmittedValue() does not return null , it will be displayed instead of it regardless of the value of the (modified) model.

Basically, if you want to stick with ajax for the cancel button, you need to exclude inputs from processing instead of relying on immediate="true" (which is actually a kind of hack). Under standard JSF conditions <f:ajax> you can do this with execute="@this" (which is actually the default) on the button instead of execute="@form" . By default, PrimeFaces buttons are @form , so you need to change this:

 <p:commandButton value="Cancel" actionListener="#{formBean.cancelForm}" update="main" async="true" process="@this" /> 
+17
source

All Articles