Site icon akquinet AG – Blog

How to access JSF from an EJB with JBoss Enterprise Application Platform 6 (aka JBoss AS7 / Wildfly)

The good old JBoss Seam framework introduced the usage of stateful session beans (SFSB) as backing beans for JSF applications. The trick was to bind the lifecycle of a SFSB to a web context, such as the session or the request context. Meanwhile this concept was integrated into the Java EE by the Context and Dependency Injection (CDI) specification. We really like to use SFSB in JSF because it provides a comfortable way to access the logic and persistence layer with an automatic and painless transaction management.

We also like to modularize our applications by separating its different layers into different Maven modules. Thus, usually the web and application logic are bundled as EJB archives, whereas the web pages are stored in a WAR archive. All modules are combined to an application as an EAR archive. In our opinion this approach is more maintainable than to mix everything into one big WAR archive.

Sometimes the web logic has to access JSF classes, i.e. to query the locale used in the current request. To do this with the JBoss EAP 6, a particularity must be taken into account. By default in the EAP6 only WAR archives containing a JSF descriptor have access to the JSF classes, EJB jars do not.

This is due to rules for implicit class loading dependencies which are added automatically by the application server at deployment time. To access JSF classes from an EJB archive, the EJB jar has to state an explicit dependency to the faces module. This is pretty simple, if you know how to do it.

First you need to know, which module you want to access. In the EAP 6.1 the module containing the JSF API is stored in:

jboss-eap-6.1/modules/system/layers/base/javax/faces/api/main

In the module.xml file you can see the name of the module:

<module name="javax.faces.api">

Now you need to add the module name as dependency manifest entry in the MANIFEST.MF file of your EJB-archive. Using Maven the best way to do this is to adapt the configuration of the EJB-jar plugin:

&lt;plugin&gt;
  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  &lt;artifactId&gt;maven-ejb-plugin&lt;/artifactId&gt;
  &lt;configuration&gt;
    &lt;ejbVersion&gt;3.1&lt;/ejbVersion&gt;
    &lt;archive&gt;
      &lt;manifestEntries&gt;
         &lt;Dependencies&gt;javax.faces.api&lt;/Dependencies&gt;
      &lt;/manifestEntries&gt;
    &lt;/archive&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;

Voila, that is it. If the application is now deployed, the JBoss EAP 6 will allow the EJBs to access the JSF classes.

As alternative to define module dependencies, you can also use the JBoss-specific jboss-deployment-structure.xml descriptor file. Using this descriptor, you can specify the dependencies for all modules of an application. But, in our opinion a dependency as the one above should be specified in the component that needs it. Thus, we prefer the alternative we presented.

Exit mobile version