• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Set a Value as Default from p:selectOneMenu List

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to select/set any value to be shown as the page is loaded, for this in backing bean, i hv set as follows:-





but no value is shown as it loads the page.
 
Saloon Keeper
Posts: 21129
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Nasir!

It's hard for me to tell, since the PrimeFaces selectOneMenu is a lot more complicated than the basic h:selectOneMenu, and I've not done much with PrimeFaces but maybe if I start simple and work up I can give you some clues.

First, selectOneMenu/selectItem are the JSF equivalents to the simple HTML <SELECT> and <OPTIONS> elements. That is, when the JSF HTML View renderer compiles and outputs those tags, that's the HTML that will actually be sent to the remote client.

SELECT is simply the container for a set of OPTIONS. When the HTML form is posted back to the server, the selected OPTION will be sent as the value of that SELECT. SELECT is a form control, so in JSF, you have to have it inside an h:form JSF element, of course.

HTML doesn't handle objects or binary data, only text, so the OPTION gives the text to display in the dropdown list and the text value that will be returned when the form is submitted. Since JSF selectOneMenu is based on this, that means that the same thing must ultimately happen there. Instead of OPTION, you have one or more SelectItem objects, which are just helpers to pair up each label in the display with its associated value. When you use selectOneMenu to select an object rather than a simple value such as text or a number, you must therefore provide a Converter that can take a string (the OPTION value) and return the object associated with that identifying string. Unfortunately, if you're using the PrimeFaces samples page as your guide, they didn't supply a sample converter. Which may be your primary problem. JSF often doesn't report missing items as errors, since it works like HTML and assumes that if it doesn't understand something it should ignore it.

It's also possible that you have 2 different theme values (theme and theme1) and are getting them confused, but I'm not certain about that.
 
Nasir Abbas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim, Its great you replied so fast, actually, although i m following the demo given @ prime faces but i found complete solution @ github.com/primefaces/showcase/blob/master/src/main/webapp/ui/input/oneMenu.xhtml which helped me a lot understanding converter etc.  But the problem is with fetching the value from the database and showing back in the selectOneMenu when the page is loaded.  I set 4 columns values as i hv shown before but it does not show the value when it loads the page, please suggest.  I m also attaching the final output as the page loads.
Result.png
[Thumbnail for Result.png]
 
Tim Holloway
Saloon Keeper
Posts: 21129
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you need to be aware of is that backing beans are not created every time a page is displayed. Instead, they are created on-demand. If you use a bean with session scope on a page, go away, and then try and display that page again expecting the constructor or post-construct methods to supply new data, it won't work, because the old bean is still being used and those methods are related to its construction, not its display.

Session scope can help with that, but really, the best way to apply database values to a page is to inject them into the bean before navigating to the view that uses it. That accords with standard JavaBean/Inversion of Control processing, where beans don't go looking for stuff, but instead have stuff delivered to them.

JSF is a true Model/View/Controller architecture, so the page display will always match what values were in the backing bean until the client changes them, at which time, if the changes are valid, the client's changes are made to the backing bean, keeping the two in sync. So the selection is going to be whatever the value= attribute of the selectOneMenuList was set to. In the event that the value references something that's not defined in a SelectItem, a default value will be displayed.

I mentioned earlier that HTML works only with text, not objects, so you have to have a Converter to cause the proper object to be selected for a label selection. But the reverse is also true. The converter's getAsString() method must return a unique identifying string when supplied with an object. That string is what the selectItem menu uses to pull the object's values for display.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!