Just some random things that might make your JSF experience nicer

Tuesday, April 04, 2006

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


/ myPage.jsp

This will force JSF to recreate the view and presto you have a cancel button


Blogger Norberto said...


I didn't get what the name "test" is. Can you name navegation rules?

6:29 AM  
Blogger Stuart said...

See this page (MyFaces wiki): http://wiki.apache.org/myfaces/ClearInputComponents

I've used the parentComponent.getChildren().clear() in my JSF/Facelets app with success (so far).

11:02 AM  
Blogger Nani Shayam Prasad said...

Thanks a lot dude. It has worked. I have been trying to reset by keep immediate="true" but i couldn't get the reset functionality. But when i tried yours it worked great. Thanks for your blog.

1:39 PM  
Blogger Prakash Shetty Kateel said...


I have a h:dataList.
I need to add extra row by clicking 'add' button. This I have implemented using following

a4j:commandButton reRender="certificateDetailPanelGroup" ajaxSingle="true" process="certificateDetailDataTable" value="#{msg.label_add}" actionListener="#{skillCertificateTabBean.addRow}"

This works great..

But I have one more requirement.
Need to delete a row on 'Delete' button against each row.
Here I tried same.. But it is triggering validations. This I dont want. But if I put 'immediate', it wont trigger validation, but whatever edited values in other rows or other fields in page will be overridden by previous values. But its is not good..

So I want to retain the submitted values of each component, but need to delete row & no validation to be triggered.

So I have an idea that, in 'deleteRow' actionListerner method, I need to make validation fail forcibly & delete row by component binding..
Can you suggest me the way to force validation fail.

Thanks for help.

3:47 AM  
Blogger MosheElisha said...

When using richfaces' a4j you can also use the combination of bypassUpdates="true" and immediate="true" on your cancel button to prevent the validation and the update of the bean.

4:27 AM  
Blogger Prasenjit Purohit said...

Thanks a lot, that really helped...

2:31 AM  

Post a Comment

<< Home