This entry is a follow up to TOTD #95 and shows how to use the recent integrations of JSR 299 in GlassFish v3 to convert a JSF managed bean to a JSR 299 bean (aka Web Beans). The TOTD #95 describes a simple Java EE 6 web application that uses Java Server Faces 2.0 components for displaying the results of a database query conducted by EJB 3.1 and JPA 2.0 classes.
The EJB class, which also acts as the JSF managed bean, looks like:
@javax.ejb.Stateless
@ManagedBean
public class StateList {
@PersistenceUnit
EntityManagerFactory emf;
public List getStates() {
return emf.createEntityManager().createNamedQuery(”States.findAll”).getResultList();
}
}
Three changes are required to convert this class into a JSR 299 compliant bean (Web Bean) as listed below:
- Add an empty "beans.xml" to the WEB-INF directory.
- Replace "@ManagedBean" with "@javax.inject.Named annotation". "@javax.inject" annotations are defined by JSR 330.
- Resource injection does not work with JPA classes, yet, so populate EntityManager explicitly as explained below:
- Replace EntityManagerFactory resource injection:
@PersistenceUnit EntityManagerFactory emf;with:
EntityManager emf = Persistence.createEntityManagerFactory("HelloEclipseLinkPU"); - Add the required entity classes explicitly to "persistence.xml". If the persistence unit is injected then the container automatically scans the web application root for any entity classes.
- Expand "Configuration Files" and edit "persistence.xml".
- Uncheck "Include All Entity Classes in …" check box.
- Click on "Add Class…", select "state.States", and click on "OK".

- Replace EntityManagerFactory resource injection:
That’s it, re-deploy your application and now you are using the Web Beans integration in GlassFish v3 instead of JSF managed bean. The output is available at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" as shown:

This is the exact same output as shown in TOTD #95.
Now, one-by-one, JPA, EJB, Transactions and other components will start working. Read Roger’s blog for another example of Web Beans in GlassFish.
A complete archive of all the tips is available here.
Technorati: totd glassfish v3 mysql javaee6 javaserverfaces webbeans jsr299 netbeans
Related posts:- TOTD #129: Managed Beans 1.0 in Java EE 6 – What and How ?
- TOTD #123: f:ajax, Bean Validation for JSF, CDI for JSF and JPA 2.0 Criteria API – all in one Java EE 6 sample application
- TOTD #144: CDI @Produces for container-managed @Resource
- TOTD #134: Interceptors 1.1 in Java EE 6 – What and How ?
- TOTD #49: Converting a JSF 1.2 application to JSF 2.0 – @ManagedBean
Hi,
when I write a java prog. in IDE, for some of them 2 files with .sig & .rs are saved. what is this 2 extension? and randomely I get "error parsing file." Please help me.
Comment by MJ — October 3, 2009 @ 8:13 pm
Please post your question to nbusers@netbeans.org with more details.
Comment by Arun Gupta — October 5, 2009 @ 1:58 pm
Arun
During the JavaOne conference I requested the JSR 299 reference implementation for non J2EE containers. Do you have any update on that. We are trying to use JSR 299 within tomcat environment, Please let me know
thanks
Comment by Sendhil Chokkalingam — November 10, 2010 @ 2:33 pm
Sendhil,
JSR 299 can only be used in Java EE 6 compliant app servers. However I believe Weld has an extension that allows you to use in Java SE environments as well.
Check http://docs.jboss.org/weld/reference/latest/en-US/html/ for more details.
Comment by Arun Gupta — November 15, 2010 @ 3:00 pm