Google

Friday, October 26, 2007

THE AJAX COMMAND STRATEGY FOR JSF

As Java Server Faces continues to mature and its component libraries grow more robust, it becomes an ever more compelling option for enterprise Web application development. Ajax has passed the stage of being the hot new thing to become an expected aspect of competitive Web applications. Marrying the two isn't necessarily difficult, but it does present some interesting puzzles. In this article, Java developer Matthew Tyson tackles one such puzzle: how to handle a single (or similar) Ajax request action across multiple JSF components, without duplicating code in the components.

I recently encountered a problem at work that had me delving into the Gang of four design patterns for a solution. I was working on a tree component that rendered its nodes according to nested facets. The user could delete a node by clicking on a Delete button or by right-clicking and selecting Delete. The delete was to be carried out via an Ajax call.

Coding all this was relatively easy, but for one problem: I needed to delete objects outside the tree, for instance, from the item detail page and from the toolbar. I didn't want to embed the deletion request logic in the tree component's renderer. I needed the logic to be accessible anywhere; from links, buttons and images, anything that could handle an onClick.

Describing my solution to a colleague the next day took quite a while. Once I had successfully conveyed the design, my coworker paused for a moment and said, "Oh, you mean it's a Command?"

The Command pattern is a convenient way to express the intention of the AjaxCommand strategy, though there are some differences. I'll focus on the AjaxCommand strategy in this article, but use the Command pattern as a point of reference.

Note that I assume you are familiar with JSF and Ajax development in general. I've used the Dojo Library for my example, but the idea applies regardless of how you accomplish your Ajax calls.

0 comments: