my dog learned polymorphism*
The moose likes JSF and the fly likes Setting select items programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Setting select items programatically" Watch "Setting select items programatically" New topic
Author

Setting select items programatically

Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Hi everybody

Is it possible to set the select items of an HtmlSelectOneMenu through actual Java code, instead of binding it in your JSF page? Thanks a lot for your help!



"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Bump...
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Doesn't anyone know whether this is possible? It doesn't seem like an unreasonable requirement to me. (Please keep in mind that I am new to JSF) Perhaps Ed Burns and Chris Schalk can help?
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Okay, I'm back again, and I think I've made a little bit of progress.

I tried to dynamically add children to the HtmlSelectOneMenu, but I get a NullPointerException after clicking the button on my test page.

Stacktrace:


I've updated my backing bean and JSP page as follows.


I'd really appreciate it if someone could offer me some advice. Thanks!

Regards,
Riaan
Ed Burns
author
Ranch Hand

Joined: Sep 11, 2006
Posts: 82
    
    5
Hello Riaan Nel,

Thanks for your question. When manipulating the view programmatically you must be aware that you're taking the reins from the tag layer at a fundamental level. Therefore, you must be very aware of how the lifecycle works with respect to the timing of when properties are written to and read from. There's nothing wrong with doing this, but it's just a concern you have to be aware of.

First, make sure TestBean is in request scope. Second, make the getter for the menu property lazily initialize the menu ivar to a new instance of HtmlSelectOneMenu, populated as you've done in your ActionListner (you don't need the actionListener for this purpose any more). Third, make sure to call setValueExpression("value", yourExpression") to the place where you want the selected value to be stored.

Does that work?

Ed
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Hi Ed

Thank you very much for your reply. I changed the scope of my bean from session to request scope, and it works like a charm. Also, thank you for suggesting lazy initialization for my variables, I'll keep that in mind. Once again, many thanks for your help!

Regards,
Riaan Nel
 
jQuery in Action, 2nd edition
 
subject: Setting select items programatically
 
Similar Threads
fill selectOneMenu from database
f:selectItem
f:selectItems can't set value
using ajax4jsf with dropdawn list
No Form Input and NullPointerException