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
"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
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?
"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
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
"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
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.
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
"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
this llama doesn't want your drama, he just wants this tiny ad for his mama