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:
module.xml file you can see the name of the module:
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:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <configuration> <ejbVersion>3.1</ejbVersion> <archive> <manifestEntries> <Dependencies>javax.faces.api</Dependencies> </manifestEntries> </archive> </configuration> </plugin>
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.