ganesh
ganesh

Reputation: 11

Java to get an instance of ManagedBean By its name

How to get an instance of a managed bean whose scope is request in JSF??

Upvotes: 1

Views: 1146

Answers (2)

Ivan Bochko
Ivan Bochko

Reputation: 117

For jsf 1.2 I use in code next expression:

    MyBean identifier =  (MyBean) context.getApplication().getELResolver().getValue(context.getELContext(), null, "myRequiredBean");

where "myRequiredBean" - beans defenition in faces-config.xml

Upvotes: 2

TC1
TC1

Reputation: 1

If you don't specify a specific name in the @ManagedBean(name = "name") annotations name attribute, the name defaults to the class' name with it's first letter lowercased. Same thing if you use JSF <2.0, but it uses xml instead of annotations.

You can get anything you need from that managed bean then using EL, which in JSF is #{beanName.field}. If using EL 2.2+, you can call a method with #{beanName.method('param')}.

Also, for the future -- you really should specify the JSF version and containers that you run it in.

Upvotes: 1

Related Questions