• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Problem with selectOneMenu

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Saloon Keeper
Posts: 28392
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 28392
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...



 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags while posting your code, improves readability. I've added them for you in the post above
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic