Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

How to access managed bean (JSF) from JSP

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, experts

I need to access properties like list etc initilalized in a managed bean (JSF): testBean from scriptlet section: <%>...</%> on JSP page.

TestBean has been defined in my face-config.xml as a session bean:

<managed-bean>
<managed-bean-name>testBean</managed-bean-name>
<managed-bean-class>org.test.TestBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Is it possible to do so ? If so, please advsie me how to do so.

Thanks,
yasushi

Thanks,
yasushi
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the interests of coding hygiene, I suggest that you avoid scriptlets. That's easy to do with JSF, but if you insist....

If the bean already exists, you can access it like any session-scoped attribute:

org.test.TestBean x = (org.test.TestBean) request.getSession().getAttribute("testBean");

(Note that there is nothing scriptlet-like about this code. This could be in any Java method.)

If the managed bean hasn't been created yet, this code wouldn't trigger its creation -- instead, getAttribute will return a null reference. To access a managed bean, triggering its creation when needed:

 
Yasushi Okubo
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I will try that.
yasushi
 
Yasushi Okubo
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jeff

It worked!

yasushi
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic