Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes Help - question on selectOneListbox and page reloads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Help - question on selectOneListbox and page reloads" Watch "Help - question on selectOneListbox and page reloads" New topic
Author

Help - question on selectOneListbox and page reloads

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

I have a selectOneListbox that is working just fine. However, after validation and when an error is found on the page, the page shows the values of all the fields but the dropdown lists do not show the selected value in the dropdown. The dropdown reverts to the default selected value.

Can someone tell me how to correct this behavior so that after validation the dropdowns display the selected values initially selected in the form?

Any help/direction would be greatly appreciated.

Here is one of the dropdowns in my form and how it is defined in my JSP page:

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

I am not sure if this tidbit helps or not but in another forum I was told that the scope of my backing bean maybe the issue(?). I have the scope set to "session" and not request. Could this be the problem?

Any help here would be greatly appreciated. I am stuck here.

Thank you.

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327


You need a valueChangeListener on your selectOneListbox.
When validation fails, certain components never make it to the backing bean, so you want to bypass validation for this component by using the immediate attribute.

According to docs I've read, immediate events are fired after the "Apply Request Values" phase, which is before "Process Validations".

I forgot one thing. In your valueChangeListener method, you'll have to get the current instance of the FacesContext and then call renderResponse() in that method. That will skip the rest of the life cycle. Doing this costs you a server roundtrip, but I don't know of another way. Maybe someone else might.


Thanks, leo
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Leo, thank you so much I'll give it a try. I really appreciate you taking the time to reply with your solution. Thanks again.
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Leo - I tried creating a valueChangeListener method as indicated below:



In my JSP I added this method as you stated above (sorry I'm working with a different dropdown that I initially put in my post above but it's the same thing):



When I run the code the new method is run first before any validations are done but what happens is that the page is reloaded the dropdown still does NOT show the selected value. Apparently I'm doing something wrong. I'll check the setter again perhaps that is where my problem is.

I hope I'm going down the correct path here. Thank you.
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Question. Is there any way that I can get an event called AFTER the validation has occurred in order to set the setter? When I do it before I still don't retain the value.

Thanks.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Melinda.

Ignore what I told you. I have trouble understanding what people are trying to ask.

When I scrape together a simple page with a text field, selectOneMenu and a command button, I don't experience the problem of the select one menu reverting to the default upon a validation error.


Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Thanks for the response, Leo. I'll try it. Also, I am assuming that selectOneMenu acts the same as selectOneListbox? Just wanted to confirm.

Thanks.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
JSF menus and listboxes both render HTML select elements.
http://download.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Leo. Question, as I am still having the same issue.

What did you put in the selectionChanged() you have below:



Here is what I had put in my valueChangeListener method:



I put in the value and value2 just to see if the values were still populated AFTER the FacesContext.getCurrentInstance().getRenderResponse() since after my page reloads the dropdown box is still NOT selected.

Here is what is in my JSP page for this selectOneListbox:



I know I must be missing something but I'm just not sure what. I tried your example and the only this is that my list is getting populated via the database which should NOT be an issue.

Here is the code as to how I'm populating my dropdown:



It is just returning a simple list. I just cannot seem to find what my problem is. Thanks again for your help.



Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

I finally resolved my problem. The issue was when the codes loaded back after the page loaded I had been setting the DEFAULT values when getting the reason codes.

Thanks for all your help, Leo. It is very much appreciated.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help - question on selectOneListbox and page reloads