Use Ajax4jsf with Seam + Facelets
Monday, September 11th, 2006Ajax4jsf is a very nice open source framework for adding Ajax features into existing JSF components. I have been playing with it in my Seam + Facelets applications. It is great fun. However, as most other Ajax JSF components, Ajax4jsf requires some tweaking to get around the JBoss ClassLoader when working with Seam. The official docs recommend that you put all Ajax4jsf and Facelets library jars into $JBOSS_HOME/server/default/lib as shared libraries. However, the problem is that those frameworks are not designed to be shared. If you have multiple web applications deployed on the same server, the shared lib approach might causes some problems. In this post, I present an alternative approach to use Ajax4jsf inside Seam.
The short answer:
- Bundle the ajax4jsf-seam-war.jar and jsf-facelets-seam-war.jar (if using Facelets) in your WAR file’s WEB-INF directory.
- Bundle the regular required JAR files of Ajax4JSF and Faclets in the EJB3 JAR file in your EAR file.
- Follow instructions in Ajax4JSF docs to setup web.xml and faces-config.xml to use Ajax4JSF with Facelets.
The explanation:
The problem with AJAX JSF frameworks and Seam is that the framework libraries need to be “seen” from both the WAR and EJB3 JAR files. (Non-Ajax frameworks do not have this requirement since they do not need to access the EJB3 “backing beans”.) But libraries in the WAR file is invisible outside of the WAR. So, the solution is to place the libraries in the EJB3 JAR file, which on the classpth of all components in the EAR. But it is not that simple: JSF only searches the WAR for tag definitions and configuration files. So, all the .tld, .taglib.xml, and faces-config.xml files in the library JAR’s META-INF directory needs to re-pakcaged into a seperate JAR and be placed into the WAR file. That’s where the *-seam-war.jar files come from.
But then, why do we need to re-package jsf-facelets.jar as well? The reason is that Ajax4JSF needs to load Facelets by itself. So, it needs to “see” jsf-facelets.jar from outside of the WAR as well.