Cancel buttons an easy way
Lets start by saying what a typical cancel button would do?
So you need it to bypass validation check we can do that with the immediate attribute, but you might also want to update the variables e.g. repopulate your variables from a database etc.
And here lies the awkward position we are placed in.
Say your page failed validation you need to call FacesContext.getCurrentInstace().renderResponse() to bypass your validation phase. But JSF does not like to update the values held in your UIComponents. It seems calling renderResponse will leave you in the situation that your variables and values held in your components don’t match.
Now what they tell us to do is put immediate = true on all the components that will have their variables updated when the cancel button is clicked.
No thanks JSF !
That means the setters will be called before the validation phase? Yuk!
What a pain in design this is, say you want to update your entire page that means every component needs immediate = true? Seems a bit silly to me.
The solution I found :)
Place immediate = true on your cancel button this is a must. Need the button to execute before the validation phase.
But then execute a navigation rule lets call it “test”
Where test navigates from myPage back to myPage where the cancel button is
This will force JSF to recreate the view and presto you have a cancel button