Archive for the ‘Ajax’ Category

Adding Ajax Behavior to the Displaytag Library

Sunday, October 14th, 2007

The JSP custom tag library Displaytag is very good at one thing – rendering HTML tables including links for paging, sorting, and data export to different formats (CSV, Excel, XML). This is very useful for building JSP views but its value is diminished on pages with Ajax behavior because each of the generated page and sort links cause the entire page to be reloaded.

You can use AjaxTags – another JSP custom tag library to make a Displaytag table re-render itself through an AJAX request and that works fine but eventually you might outgrow the approach of wrapping JavaScript code with JSP custom tags and may want to gain further control of client-side behavior. Furthermore AjaxTags requires use of the Prototype JavaScript library, which may or may not suite your deployment scenario.

Actually adding AJAX behavior to a Displaytag table is not that difficult. The code shown below uses the Dojo toolkit but the demonstrated technique can be implemented with any of the other JavaScript toolkits that have remoting capabilities. This is what you need to:

  1. Add a div tag around the Displaytag table.
  2. Declare the JavaScript function shown below and pass the div id to it.
function ajaxifyDisplayTag(targetId) {

  var onclickHandler =
    function (event) {
      event.preventDefault();
      dojo.xhrGet({
        url: event.target.href,
        load: function(data){
            dojo.byId(targetId).innerHTML = data;
            dojo.parser.parse(dojo.byId(targetId));
            ajaxifyDisplayTag(targetId);
        },
        error: function(data){
            alert("HXR Error in ajaxifyDisplayTag" + data);
        },
    });
  };

  dojo.forEach(
    dojo.query('a', dojo.byId(targetId)),
      function(aTag) {
        console.debug('Set onclick handler for ' + aTag);
        dojo.connect(aTag, 'onclick', onclickHandler);
    }
  );
}

That’s it!

In short what the above function does is it finds every hyperlink within the specified div tag and connects a pre-defined handler to its onclick event. The handler prevents the actual event from taking place and instead fires an AJAX request to the same URL. The returned data is then used to replace the content of the target div tag. Since the returned HTML content is also a Displaytag table we need to invoke the above function on the returned HTML content to ensure its links are connected to our onclick handler.

Of course if your AJAX toolkit already has a data grid widget then you don’t need any of the above. Dojo users are about to get one very soon as part of Dojo the 1.0 release.

Notes from the Rich Web Experience Conference, San Jose CA

Saturday, September 29th, 2007

I attended a rather direct and revealing talk given by Alex Russel the project lead for the Dojo Toolkit. Given the importance of the Dojo toolkit I was curious to see what Alex would have to say. His talk, titled “Standards Heresy” with the subtitle of “Dojo and the Rise of Open Web Pragmatism” focused at length on standards – what makes a good standard, how does it relate to competition, what makes a good standards body, and so on?

So why the focus on standards? Douglas Crockford, a senior JavaScript architect at Yahoo and creator of JSON gave the keynote speech in which he pointed out a number of failures of rich web development as realized in browsers to date. The notable failures are what he calls “insecurity” or the lack of security on the browser side (e.g. any JavaScript code downloaded as part of your page has access to any other data on the page including cookes, session id’s, etc.) and the lack of advancement in presentation technology (HTML 4.0 dates back to 1999). According to him standards bodies have failed us and it is a striking conclusion considering the fact the number of mainstream browsers isn’t that big.

Continuing on this theme Alex Russel drew out the W3C specification timeline. What we have is HTML 4.01 in 1999, XHTML 1.0 in 2002, DOM 3 Core in 2004, CSS 2.1 in 2007, (CSS 2.0 dates back to 1998). HTML 5 is scheduled for late 2008 and CSS 3 according to him is a mess. Enter WHATWG (Web Hypertext Application Technology Group). This group is focused on evolving HTML in parallel with the W3C HTML working group and the resulting specifications are likely to be implemented in major browsers. Membership in this organization is much more open, there are no fees and in many ways it is much more of a grassroots movement focused on innovation.

Alex characterized ajax toolkits as “symbiotic parasites”, which have continuously pushed the bar of web evolution in terms of what can be done with what we have. This certainly rings true given the existence of ~ 200 ajax toolkits out there. There are efforts to try and bring this ecosystem of toolkits closer together through the work of the Open Ajax Alliance and to me that’s a welcome development bringing many players to the table but what remains is a lack of progress in the underlying capabilities of HTML, DOM, and CSS. Who knows maybe WHATWG will bring about much needed innovation.