Thursday, March 31, 2011

Autocompletion of JSF/CDI Managed Beans in JBoss Tools

Jboss-tools-managedbean-cdi

Current JBoss Tools 3.2 (Eclipse IDE plugin for developing Java EE web applications) is not yet able to show JSF 2.0 managed beans annotated with @ManagedBean.

For example, the code below will load just fine with JSF 2.0, but JBoss Tools will "ignore" it:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="refillManager")
@SessionScoped
public class RefillManager {

When you type the following in the Facelets XHTML template editor, you will not get a code assist hint for refillManager bean :

<h:dataTable value="#{refillManager.refills}">

However there is an alternative, which is actually a better one anyway. (hint: why use JSF-only Managed Beans when you have CDI beans that you can use everywhere?)

Use CDI's @Named annotation :

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@SuppressWarnings("serial")
@Named("refillCdi")
@SessionScoped
public class RefillCdi implements Serializable {

Now this CDI bean will be detected by JBoss Tools and will show up in autocompletion popup.

To activate CDI (Java Context and Dependency Injection) in a Java EE 6 web application, create an empty file named WEB-INF/beans.xml and you're done. :-) JBoss Tools also has excellent support for CDI.

No comments:

Post a Comment