| Author |
Clearing forms
|
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
Hi all, This should be easy, in fact it's probably so easy that I'm not seeing the answer... I have a form on a page that is linked to a session backing bean and there are two command buttons linked to the form, one to save/validate it's contents (which thankfully works fine) and a clear button which is supposed to blank the form (obciously). I've tried umpteen different ways of doing this and none of them have worked. No matter what I do, when the clear button is clicked, the screen is redrawn with the previous contents (even though the contents of the backing bean have been cleared down). At the moment the command button has the "immediate" attribute set to true to avoid validating the incoming request, and the action linked to it simply clears the contents of the backing bean. I'm I being particularly stupid and not seeing the obvious answer?!!? Any help would be greatly appreciated (and would also help keep my hair attached to my head!) Cheers, Kev
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
Well, you have told *what* you have done but not *how* you have done it. How are you trying to clear the forms that isn't working? Are you using <h:commandButton type="reset" /> or what?
|
 |
Prakash Jebaraj
Greenhorn
Joined: Mar 14, 2005
Posts: 5
|
|
Hi, <h:commandButton type="reset" /> will reset to previous values and not clear the values. So we have to reset to null in the backing bean as follows: <h anelGrid styleClass="alignLeft"> <h:commandButton type="submit" value="Clear" styleClass="commandExButton" id="id2" actionListener="#{searchAgreement.clear}" /> </h anelGrid> /** * Action Listener method for clear action * * @param eActionEvent */ public void clear(ActionEvent e) { FacesContext facesContext = FacesContext.getCurrentInstance(); UIViewRoot uiViewRoot = facesContext.getViewRoot(); HtmlInputText inputText = null; inputText = (HtmlInputText) uiViewRoot.findComponent("searchAgreement:agreementNumber"); inputText.setValue(""); inputText = (HtmlInputText) uiViewRoot.findComponent("searchAgreement:customerName"); inputText.setValue(""); inputText = (HtmlInputText) uiViewRoot.findComponent("searchAgreement:customerRefNumber"); inputText.setValue(""); inputText = (HtmlInputText) uiViewRoot.findComponent("searchAgreement:customerNumber"); inputText.setValue(""); inputText = (HtmlInputText) uiViewRoot.findComponent("searchAgreement:contactPhoneNumber"); inputText.setValue(""); } hope this helps Prakash
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
What I've done so far is use a commandButton to execute a method on the session backing bean that clears down it's contents (i.e. reset it's fields to thier default values), but for some reason the front end display components still retain their values.
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
Originally posted by Kev D'Arcy: What I've done so far is use a commandButton to execute a method on the session backing bean that clears down it's contents (i.e. reset it's fields to thier default values), but for some reason the front end display components still retain their values.
This wont work as you are clearing the bean but not the submitted values. So either use java script to clear out the submitted value / clear out the submitted values of each component OR write an action method and set immediate=true on the UI componant. In the listener method clean up the bean and immediately update UIViewroot followed by render response ... this will stop JSF from applying submitted values in request to model bean. Or open a new page all together on clicking reset button
|
- Varun
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
write an action method and set immediate=true on the UI componant. In the listener method clean up the bean and immediately update UIViewroot followed by render response ... this will stop JSF from applying submitted values in request to model bean.
This seems to be the best option, I've tried the the solution that Prakash suggested, but had no luck. How do I update the UIViewRoot object (is it the processUpdates() method?). I'm assuming the render response that you're talking about is the renderResponse() method on the FacesContext object... -- Kev
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
What a pain. JSF is definately not without it's problems.
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
Definitely not! You'd think they'd make this one easy though! Even though this is turning out to be such a problem, I'm still converted...
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
I've got another option for you. Use the binding attribute to bind your inputText components to UIInput properties in your backing bean. Then, in your clear method do something like: nameText.setSubmittedValue(""); That will clear the form fields on the page. So for example: Hope that helps. Depending on how many fields you have I don't know if that is really easier or not.
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
Originally posted by Kev D'Arcy: I've tried the the solution that Prakash suggested, but had no luck.
It will work depending upon when you are doing the cleaning i.e. in which phase. If you clean and later JSF re-applies the submitted Values (unless you clear it explicitly as suggested by Greg) to model bean , your changed would be gone. Best time to call the method suggest by prakash would be in the last phase OR call it earlier and render response immediately.
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
Originally posted by Kev D'Arcy: This seems to be the best option, I've tried the the solution that Prakash suggested, but had no luck. How do I update the UIViewRoot object (is it the processUpdates() method?).
you bet.
I'm assuming the render response that you're talking about is the renderResponse() method on the FacesContext object... -- Kev
You are right again. [ April 12, 2005: Message edited by: Varun Khanna ]
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
I found out what's been screwing me up... By calling the setValue() methods, I was only resetting the original value of the input boxes, but there is another field that they have called "submittedValue" which receives the values from the incoming request. Clearing the "value" field only clears out the original value, but at some stage later in the lifecycle, the submitted value is set into the value field. So, it appears the both of these fields need to be cleared down. -- Kev
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
Originally posted by Kev D'Arcy: I found out what's been screwing me up... By calling the setValue() methods, I was only resetting the original value of the input boxes, but there is another field that they have called "submittedValue" which receives the values from the incoming request. Clearing the "value" field only clears out the original value, but at some stage later in the lifecycle, the submitted value is set into the value field. So, it appears the both of these fields need to be cleared down. -- Kev
I didn't have that problem with the UIInput. I only have to call setSubmittedValue. In fact, I tried setValue first and it didn't clear out the textbox. What method did you decide to go with of all the suggestions?
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
Greg, Currently I'm calling both the setValue() and setSubmittedValue() and setting both of them to empty string values in an action method. I have a suspicion that this is causing a few new quirks in the front end, so what I'm thinking of doing now is trapping the click event on the button somehow, stopping the form with the details from being submitted, and sending up a blank form instead, this will allow the backend beans to be cleared without having to clear down the UIInput objects. -- Kev
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
Originally posted by Kev D'Arcy: Greg, Currently I'm calling both the setValue() and setSubmittedValue() and setting both of them to empty string values in an action method. I have a suspicion that this is causing a few new quirks in the front end, so what I'm thinking of doing now is trapping the click event on the button somehow, stopping the form with the details from being submitted, and sending up a blank form instead, this will allow the backend beans to be cleared without having to clear down the UIInput objects. -- Kev
If the pages uses complex validations etc. this almost becomes an essential. I remmeber facing some issue due to the same and clearing the fields using javascript helped.
|
 |
Kev D'Arcy
Ranch Hand
Joined: Jul 26, 2001
Posts: 75
|
|
If the pages uses complex validations etc. this almost becomes an essential. I remmeber facing some issue due to the same and clearing the fields using javascript helped.
How did you do this? I've tried doing a few fiddly things with javascript, but they're all linked to the onclick of a commandButton. My main problem is preventing the form being submitted before I can clear it down.
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
Originally posted by Kev D'Arcy: How did you do this? I've tried doing a few fiddly things with javascript, but they're all linked to the onclick of a commandButton. My main problem is preventing the form being submitted before I can clear it down.
Your button will have an action as well as an onclick. Onclick will clear your form and action will submit it. Onclick, executed by browser, will happen first then action, executed at server.
|
 |
ali hammad
Greenhorn
Joined: Dec 03, 2009
Posts: 4
|
|
|
@Prakash Jebaraj Thank you so very much, you've saved my day .. Your solution worked for me .. thanks a lot
|
 |
jeremy pppppp
Greenhorn
Joined: Dec 13, 2010
Posts: 1
|
|
|
Hey Prakash! Great work buddy. Your code is so simple! Saved my day as well.
|
 |
Gernot Krost
Greenhorn
Joined: Jul 25, 2011
Posts: 1
|
|
Another option (depending on your needs ...) from the Apache JSF Wiki
|
 |
pradeep gamage
Ranch Hand
Joined: Aug 03, 2009
Posts: 85
|
|
Thank You Prakash You Save my Life......
|
Software Engineer(BSC):SCJP 1.5
(Knowledge is power when applied)
|
 |
 |
|
|
subject: Clearing forms
|
|
|