aspose file tools
The moose likes JSF and the fly likes How to access managed bean (JSF) from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "How to access managed bean (JSF) from JSP" Watch "How to access managed bean (JSF) from JSP" New topic
Author

How to access managed bean (JSF) from JSP

Yasushi Okubo
Greenhorn

Joined: Jan 28, 2005
Posts: 9
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
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:



There is no emoticon for what I am feeling!
Yasushi Okubo
Greenhorn

Joined: Jan 28, 2005
Posts: 9
Thanks, I will try that.
yasushi
Yasushi Okubo
Greenhorn

Joined: Jan 28, 2005
Posts: 9
Hi, Jeff

It worked!

yasushi
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to access managed bean (JSF) from JSP
 
Similar Threads
Accessing jsf bean from another jsf bean
how to access managed bean (JSF) from JSP
is there a mayor change on how to use faces-config.xml on JSF 2.0
Extracting data from a bean
Accessing JSF sesson bean from a servlet