The AjaxCommand strategy for JSF

Update the Command pattern to handle common Ajax requests

As JavaServer 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.

A Command pattern for Ajax?

In the canonical example, the Gang of Four Command pattern is used by a toolkit developer to enable future users to specify actions within the toolkit. Not knowing how the toolkit will be used, the developer codes to an interface, leaving the implementation details abstract.

The average enterprise Web application involves a similar scenario, where one person or group provides the tools to another. Although the typical Web application probably doesn't need to provide such complete autonomy between teams, other factors at work in a JSF-Ajax call suggest the use of the Command pattern; namely the layered architecture, the intermediary of HTTP, and JSF's component model.

Now, let's say you're working on a Web application where you need to do the following:

  1. Handle commands arriving as Ajax requests without isolating the logic in a component.
  2. Make it easy to add new commands.
  3. Delegate the business logic to the business layer.

We'll start by taking a look at how you would set up the server side of the request, based on the AjaxCommand strategy. After that we'll look at the front-end design, and I'll conclude by comparing the original Command pattern with the AjaxCommand variation.

1 2 3 4 5 Page 1
Page 1 of 5