[EJB 3.0] - Re: @EJB Injection in JSF Managed Bean
by klejs
If you don't want your code full of EJB dependent code you can use a VariableResolver instead to "inject" your EJBs into your Managed Beans. It would look something like this:
public class SessionBeanVariableresolver extends VariableResolver {
|
| private Context context;
|
| protected final VariableResolver resolver;
|
| public SessionBeanVariableResolver(VariableResolver resolver) {
| super();
| this.resolver = resolver;
| try {
| context = new InitialContext();
| }
| catch(NamingException e) {
| // handle exception
| }
| }
|
|
| @Override
| public Object resolveVariable(FacesContext context, String name)
| throws EvaluationException {
|
| // Return if object found with regular resolver
| Object result = resolver.resolveVariable(context, name);
| if(result != null) {
| return result;
| }
|
| try {
| result = context.lookup(name);
| }
| catch (NamingException e) {
| // handle exception
| }
|
| return result;
| }
| }
|
In your backing beans you should have a setter for the EJB like this:
public Class MyManagedBean() {
|
| private MyEjbInterface bean;
|
| public void setMyEjbInterface(MyEjbInterface bean) {
| this.bean = bean;
| }
| }
And finally in your faces config you should have an entry for your variable resolver:
<variable-resolver>my.package.SessionBeanVariableResolver</variable-resolver>
and on you managed bean you should set a managed property like this:
<managed-bean>
| <managed-bean-name>MyManagedBean</managed-bean-name>
| <managed-bean-class>my.package.MyBean</managed-bean-class>
| <managed-bean-scope>request</managed-bean-scope>
| <managed-property>
| <property-name>myEjbInterface</property-name>
| <value>#{path_to_ejb}</value>
| </managed-property>
| </managed-bean>
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3990206#3990206
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3990206
18Â years, 4Â months
[JBoss Seam] - ScopeType.EVENT versus getter method actionBean.getProperty(
by antispart
Am I correct in my understanding that the following two are effectively the same? the property is outjected into event scope so it's called several times during the JSF request processing lifecyle, and so is the the actionBean.getProperty() method - and both have the same lifetimes?
ActionBean.java:
|
| @Stateful
| @Name("actionBean")
| public class ActionBean implements ActionBeanLocal (
| ....
| public getProperty()
| return property();
| }
|
| ...
|
| example.xhtml:
|
| <h:outputText value="#{actionBean.getProperty}"/>
and
ActionBean.java:
|
| @Stateful
| @Name("actionBean")
| public class ActionBean implements ActionBeanLocal (
| ....
| @Out(scope=ScopeType.EVENT)
| public getProperty()
| return property();
| }
|
| ...
|
| example.xhtml:
|
| <h:outputText value="#{property}"/>
When is one pattern preferable to the other?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3990201#3990201
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3990201
18Â years, 4Â months