Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with selectOneMenu

 
J Na
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JSF2.1 & Apache Tomcat 7.

I try a simple task in JSF.

My facelet view contains two selectOneMenu components & a commandButton.

First selectOneMenu displays three state names (e.g Gujarat, Rajasthan & Maharashtra). When the user selects any one state from first list, the second list should be populated with three cities from the selected state. (e.g If the user selects Gujarat, the second list should be populated with Ahmedabad, Surat & Rajkot only).

If the user clicks on commandButton to submit the form, selected state & city should be displayed below the same form.

I'm trying the following code, BUT IT DISPLAYS THIS ERROR: j_idt7:menu_city: Validation Error: Value is not valid

Facelet View -----------------------------------------------




Backing Bean ----------------------------------------------


PLEASE HELP ME OUT...THANK YOU IN ADVANCE.

 
Tim Holloway
Bartender
Posts: 18422
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch! But can you set your display name to something a little less abbreviated? Thanks.

Also, you will find a "Code" button on the Ranch message editor. You can use it to make code and XML samples easier to read.

I think your main problem is that you need more understanding of the JSF model architecture and the JSF life cycle.

Because an HTML SELECT OPTION has 2 components (display value and data value), the corresponding JSF element (selectItem) is not a simple string, it's a special JSF model wrapper. In fact, it's the SelectItem class. Rather than define each element on the View Definition, it's often easier to define the whole item list as a unit. So rather than a View definitions for "info.states[0]", "info.states[1]" and so forth, you can define a collection of items using the "f:selectItems" tag, and have it reference a List or array of SelectItem objects fetched as a unit ("info.states").

The selectItems must be initialized BEFORE the View is displayed. If you attempt to set a SelectOneMenu to a value that isn't in that set of selectItems, that is invalid.

Rather than force submission of the entire form, it's also usually preferable to use AJAX. AJAX can submit partial forms, eliminating the problem you get that if even one (unrelated) control value on the form is not valid, the entire form is rejected and the valueChangeListener won't fire. AJAX support is built into JSF2.

What I normally do when cascading selectOneMenus like this is make the valueChangeListener null out the lower-level selectItem collection and make the corresponding "get" method for that collection automatically (re)build the selectItem collection based on the higher-level value in cases where that collection was found to be null.
 
J Na
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Tim for your comprehensive reply..

Last evening, I changed the scope of the backing bean from RequestScope to ViewScope & to my surprise the problem got fixed !

Will you please explain the role of the scope in this issue?
 
Tim Holloway
Bartender
Posts: 18422
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request scope isn't 100% useless in JSF, but it's close.

The problem is, JSF is heavily based on the concept of "postbacks", where the same form is repeatedly submitted until the user passes all validations. View (also Session and Application) scope allow carrying over important context information between postbacks. In your particular case, that would have included the higher-level selections. Unfortunately, Request scope objects are created, destroyed, and re-created repeatedly. Every time you submit a form, a brand-new instance of the object is built from scratch, employed to process and render, then immediately discarded.

With View scope, the bean is retained - including its accumulated internal state - and re-used as long as that particular View is being used.
 
J Na
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Once again, thank you for your reply.

Later, I also incorporated AJAX into JSF as you had suggested. And I noticed that the 'render' attribute matters a lot while using AJAX.

Your comments please...



 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags while posting your code, improves readability. I've added them for you in the post above
 
Let nothing stop you! Not even this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!