The AjaxComponent strategy for JSF: The best of both worlds

Ajax-enable your JSF components with a phase listener

1 2 3 4 5 6 7 Page 7
Page 7 of 7

Building and deploying the sample application

The example application uses Maven 2 for its build tool. Once you've got Maven installed, browsing to the root of the application and typing mvn clean install will compile the class files and package them into a WAR file, along with the descriptors and config files from the webapp directory.

If you are familiar with Ant, Maven can take a little getting used to, but it saves time by defining a typical structure for things (like a Web app) and automating tasks around that structure. If you develop with Eclipse, note that you can tell Maven to build all your .project and .classpath files by using mvn eclipse:eclipse. You should also be aware that when Maven generates these files, it uses an environment variable called M2_REPO that you'll want to add to Eclipse. M2_REPO points to the root of the Maven repository.

The Maven repository holds all the files that your builds have requested. The first time you request a file, it has to go out to the Internet to find it, using the artifact ID and version provided in pom.xml. The latter file is what defines the build process. (This quick description isn't intended to be thorough; if you need more information on Maven, see the Resources section.)

Once you've run mvn clean install, the WAR file is ready for deployment. You can find it in the target directory, as jsf-ajax-component-1.0.0.war. Just copy the WAR file into Tomcat's webapp directory, and browse to http://localhost:8080/jsf-ajax-component-1.0.0.

The best of both worlds

The AjaxComponent strategy described here is very effective for use in production components. Using this technique, you can add Ajax capability to JSF components relatively easily, and keep all of your input/output logic nicely packaged in the renderer. Once the phase listener is set up, it's very easy to add more components that use the strategy -- in fact, you don't even have to touch the listener.

If you find that putting the request handling logic into the component itself doesn't really make sense, then you should probably use the JSF AjaxCommand strategy instead. You can even use the same phase listener. With the two strategies in place, you are ready to handle any kind of Ajax request from your JSF application.

A good guideline for choosing between these two strategies is to ask yourself whether the functionality you are providing will be required by just one component, or many. If the answer is many, then your application is a good candidate for an AjaxCommand, since you will then be able to share the functionality across all the components without duplicating the code. Otherwise, go with the AjaxComponent, and keep the code tight and encapsulated.

As always, let me know your experience and thoughts. Join the discussion below to let me know what you think!

Matt Tyson has been building Java Web applications for 10 years. He runs a software consultancy called Odyssey Design and Development, Inc.

Learn more about this topic

This story, "The AjaxComponent strategy for JSF: The best of both worlds" was originally published by JavaWorld.

Copyright © 2007 IDG Communications, Inc.

1 2 3 4 5 6 7 Page 7
Page 7 of 7