• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Setting select items programatically

 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bump...
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 82
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic