Pages

Sunday, January 1, 2012

WebLogic 12c now supports Java 6 so we can now try out the JSF 2.0 Managed Bean annotations together with CDI JSR-299. In this blogpost I will use OEPE 12c as my IDE and deploy everything on WebLogic 12c. Off course I will tell you my experiences to get all this working in Eclipse and WebLogic 12c.

First let's start with the JSF 2.0 Managed Bean Annotations.

We need to remove the managed bean definitions from the faces-config.xml file. Then make sure that you don't use the metadata-complete attribute ( metadata-complete="true" ) on the faces-config element, this will disable the search for JSF Managed Beans.

Then we can add the JSF Managed Bean annotations to the java class. We can use @ManagedBean ( javax.faces.bean ) together with the right scope annotation like RequestScoped, SessionScoped or ViewScoped ( CDI does not have this View Scope ). You will see this bean in the Faces Configuration View.

Next part of this blog is about CDI, this will be more tricky, it is possible but does not work so well in OEPE and WebLogic. I didn't have these problems with the same OEPE, Code and using Glassfish 3.11.

So to enable CDI we need to add a beans.xml file to the WEB-INF folder. Like this with a empty beans element, this will trigger CDI.

When we try to run the JSF application again then you probably will hit this NPE in the jboss Weld framework.

The solution is to remove the JSP entries and rename your files from jspx to xhtml.

From this.

<?xml version="1.0" encoding="iso-8859-1"?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:trh="http://myfaces.apache.org/trinidad/html"

xmlns:tr="http://myfaces.apache.org/trinidad">

<jsp:directive.page contentType="text/html;charset=utf-8"/>

<f:view>

<tr:document title="Bean and ExtVal validation">

<tr:form>

To this, where we define the used jsf namespaces on the html element.

Next we can try to create a new managed bean where we will use the Named annotation together with the SessionScoped annotation of javax.enterprise.context

We can also inject an EJB or a Managed Bean with the Inject annotation.

I can also replace @EJB with @Inject but then I should move my EJBs to my Dynamic Web Project else I will get an Weld error. Probably with OEPE you have an different JPA project and this ejb.jar will be deployed together with the WAR in an EAR.

I didn't test this but you need to enable CDI on this ejb project or EAR.

That's not all you also need to change the WebLogic Publishing mode of OEPE. This should be Publish as an exploded archive else Inject won't work. ( Thanks to Steve Button for the tip. ).

This is the error I got WELD-001408 Unsatisfied dependencies for type [DataBeanCDI] with qualifiers [@Default] at injection point [[field] @Inject private nl.amis.web.beans.DataBean.dataBeanCDI]

This is solved in the WebLogic 12c (12.1.1) patch release of March 2012, so no need for an exploded archive

And now the most annoying bug, the CDI managed beans will work only once on WebLogic 12c. When you deploy or run your application the second time then the CDI Managed bean can't be found. The solution is you need to restart the WebLogic Server. This makes development with CDI almost impossible.

This is also solved in the WebLogic 12c (12.1.1) patch release of March 2012

I also added and removed a patch which makes everything even a little better

ex Oracle ACE , Java Developer of the year 2009. Co-writer of the OSB Development Cookbook.I am working as a Software Architect and specialized in CI,CD, integration, middleware, security and web development.