Java to get an instance of ManagedBean By its name
开发者_Python百科How to get an instance of a managed bean whose scope is request in JSF??
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
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.
精彩评论