6 October 2007

DWR

I can't heap enough praise on the JavaScript library DWR (direct web remoting). It came to my attention a few months back, but I've only now been able to really dive in an use it. DWR dynamically generates JavaScript classes from the Java classes of your choice. Any calls beyond simple set/get are implemented as Ajax calls back to the server. DWR allows you to leverage your existing Java code without reimplementing in JavaScript.

Given the Java class Users that manages system users with the User class, we could have the following simplified script in a page:

<script type="text/javascript">
    // Ajax call to server with JavaScript callback
    Users.findActive(showActiveUsers);

    // Callback passed collection of User objects, populate page as needed
    function showActiveUsers(users) {
        $A(users).each(function(user) {
            new Insertion.Bottom(
                "myDiv",
                "<p>" + user.name + ": " + user.id + "</p>);
        })
    }
</script>

Voila. JavaScript code becomes an extension of your Java code (with a little help from the equally invaluable Prototype library).

[ posted by sstrader on 6 October 2007 at 12:35:44 PM in Programming ]