permaculture playing cards*
The moose likes JSF and the fly likes Wierd problem while using valueChangeListener() during submit of a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Wierd problem while using valueChangeListener() during submit of a form" Watch "Wierd problem while using valueChangeListener() during submit of a form" New topic
Author

Wierd problem while using valueChangeListener() during submit of a form

Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Hi Friends,

I apologize for this is the shortest description that I could write about this issue.

This is my JSP code:


In the above code the productLineGroupMenu drop-down populates on the basis of the value selected in productLineMasterGroupMenu. The valueChangeListener called is masterGroupChange() of ProductLineBean.
When I click in "Save" button to finally submit the form it doesn't invoke saveProductLine() method which is responsible for saving the form data to the database. If I remove the valueChangeListener from productLineMasterGroupMenu then the productLineGroupMenu is not populated on the change of productLineMasterGroupMenu but then I am able to save the form data by clicking the "Save" button i.e. at that time saveProductLine() method is called perfectly fine. I just wonder what prevents saveProductLine() method from being invoked when I use valueChangeListener on productLineMasterGroupMenu.
Here is the code for the method masterGroupChange() which I use as the valueChangeListener:


Here is the code for saveProductLine() method which is called when "Save" button is clicked:


I also used a PhaseListener to track what phases are executing when I run the above application.
The following is the meaningful log which was generated when I did not valueChangeListener and when I clicked the "Save" button to save the form:
#### NITIN BEFORE: RESTORE_VIEW(1)
### NITIN AFTER: RESTORE_VIEW(1)
#### NITIN BEFORE: APPLY_REQUEST_VALUES(2)
Hi Nitin inside BaseDaoImpl-->createCriteria() method
clazz.getCanonicalName()=com.stryker.corp.coa.model.ProductLineGroup . Creating Criteria for this class and returning it
Hi Nitin inside ProductLineBean --> getProductLine() method
### NITIN AFTER: APPLY_REQUEST_VALUES(2)
#### NITIN BEFORE: PROCESS_VALIDATIONS(3)
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
326743 [http-8181-Processor22] INFO com.stryker.corp.coa.web.converter.ShortDateConverter - No date supplied - returning null
Hi Nitin inside ProductLineBean --> getProductLine() method
326743 [http-8181-Processor22] INFO com.stryker.corp.coa.web.converter.ShortDateConverter - No date supplied - returning null
Hi Nitin inside ProductLineBean --> getProductLine() method
### NITIN AFTER: PROCESS_VALIDATIONS(3)
#### NITIN BEFORE: UPDATE_MODEL_VALUES(4)
Hi Nitin inside ProductLineBean --> setMasterGroup() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean --> getProductLine() method
### NITIN AFTER: UPDATE_MODEL_VALUES(4)
#### NITIN BEFORE: INVOKE_APPLICATION(5)
Hi Nitin inside ProductLineBean--> saveProductLine() method....ProductLine DescriptionNitzy Created dummy PL Product Line ID=null
### NITIN AFTER: INVOKE_APPLICATION(5)
#### NITIN BEFORE: RESTORE_VIEW(1)
### NITIN AFTER: RESTORE_VIEW(1)
#### NITIN BEFORE: RENDER_RESPONSE(6)
Hi Nitin inside BaseDaoImpl-->createCriteria() method
clazz.getCanonicalName()=com.stryker.corp.coa.model.ProductLineGroup . Creating Criteria for this class and returning it
Hi Nitin inside BaseDaoImpl-->createCriteria() method
clazz.getCanonicalName()=com.stryker.corp.coa.model.ProductLineGroup . Creating Criteria for this class and returning it
### NITIN AFTER: RENDER_RESPONSE(6)


In above log you can see that in the INVOKE_APPLICATION phase the saveProductLine() method was called and the data was saved in database.

The following is the log when I used the valueChangeListener and I clicked the "Save" button:
#### NITIN BEFORE: RESTORE_VIEW(1) // This starts when we fill the form and submit the form for saving a Product Line.
### NITIN AFTER: RESTORE_VIEW(1)
#### NITIN BEFORE: APPLY_REQUEST_VALUES(2)
Hi Nitin inside BaseDaoImpl-->createCriteria() method
clazz.getCanonicalName()=com.stryker.corp.coa.model.ProductLineGroup . Creating Criteria for this class and returning it
Hi Nitin inside ProductLineBean --> getProductLine() method
Hi Nitin inside ProductLineBean-->masterGroupChange().... evt.getOldValue()=33 Nitzy Created Dummy PL Group
Hi Nitin inside ProductLineBean-->masterGroupChange().... evt.getNewValue()=33 Nitzy Created Dummy PL Group
### NITIN AFTER: APPLY_REQUEST_VALUES(2)
#### NITIN BEFORE: RENDER_RESPONSE(6)
Hi Nitin inside BaseDaoImpl-->createCriteria() method
clazz.getCanonicalName()=com.stryker.corp.coa.model.ProductLineGroup . Creating Criteria for this class and returning it
Hi Nitin inside ProductLineBean --> getProductLine() method
### NITIN AFTER: RENDER_RESPONSE(6)

In above the INVOKE_APPLICATIONS Phase was not executed and the method masterGroupChange() which is the valueCHangeListener got executed instead of that.
I can't determine what is the cause of this ambigous behaviour. Please help !
Thank You!
Nitin

Vaibhav Mittal
Greenhorn

Joined: Jun 11, 2008
Posts: 23
I think the statement ctx.renderResponse(); in the method masterGroupChange is causing this behavior. This is what is happening according to me- Since the method is actionListener it is called when the value of the master select menu group is changed. Now in this method the directly the renderResponse() is called which skips the Invoke Application phase which is when the action method that is saveProductLine() is to be called. You can also try using ajax4jsf tags instead of the actionlisteners if the problem persists.


"Life is good"
Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Thanks Vaibhav for taking out some time to reply. However, the method masterGroupChange() is a valueChangeListener and not an action Listener. It is called when we change something in the related dropdown so as to change the masterGroup field of ProductLine bean on the basis of which the next drop-down is populated. The ctx.renderResponse() makes sense in masterGroupChange() because we have to skip the ProcessValidations phase when we change the Master Group in that dropdown. The Action Listener is saveProductLine() method which is registered on the "Save" button click.
The problem is that when we click "Save" button then saveProductLine() is not called but masterGroupChange() method is called.
Views?
Thanks,
Nitin
Mohit MamChand Garg
Greenhorn

Joined: Apr 30, 2013
Posts: 1
Any one got solution to this?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961
    
  19

JSF Rule #1: The more JSF-specific your solution is, the more likely it's not a good solution.

First, however, unless you are stuck with JSF 1.x and are forbidden to take advantage of any available extensions such as RichFaces or PrimeFaces, you should remove the 'onchange="submit()"' and replace it with an AJAX request. One that submits ONLY the changed item. A standard submit not only will refresh the entire web page, but it will be hostage to all of the other control values on the form. Meaning that if even one entirely unrelated control value is invalid, your change will be rejected.

A much cleaner way of cascading selectMenu lists is to remove the JSF-specific code in the valueChangeListener and use POJO code. It's as simple as this:


To make this work, the "getProductLineGroupSelections" method should check for a null productLine.parentProductLineGroup value, and if it's null, construct a new productLine.parentProductLineGroup SelectItem model list based on the master selection value. I have found that constructing models on-demand when referenced makes the code much simpler and more maintainable.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wierd problem while using valueChangeListener() during submit of a form
 
Similar Threads
Jumping past phases 2, 3, 4, and 5?
Unable to call Action Methof
[JSF 2] internationalization: UPDATE_MODEL_VALUES et INVOKE_APPLICATION phases are skipped
Issues with facelets and saveState?
JSF 2.0 selectOneMenu control not populated